include

include

include

include

using namespace std;
const int L=110;
int C[L],vis[L][L],map[L][L],dis[L];
bool b[L];
int main(){
memset(map,0,sizeof(map));
int N,K,M,S,T;
cin>>N>>K>>M>>S>>T;
for(int i=1;i<=N;i++)cin>>C[i];
for(int i=1;i<=K;i++)for(int n=1;n<=K;n++)cin>>vis[i][n];
for(int i=1;i<=M;i++){
int x,y,d;
cin>>x>>y>>d;
map[x][y]=d;
map[y][x]=d;
}
memset(dis,-1,sizeof(dis));
for(int i=1;i<=N;i++){
if(i==S)dis[i]=0;
else if(map[S][i]!=0){
if(vis[C[i]][C[S]]==0)dis[i]=map[S][i];
}
}
b[S]=1;
for(int i=1;i<N-1;i++){
int xmin=INT_MAX,k=-1;
for(int n=1;n<=N;n++)if(!b[n]&&xmin>dis[n]&&dis[n]!=-1){
xmin=dis[n];
k=n;
}
if(k==-1)continue;
b[k]=1;
for(int n=1;n<=N;n++)if(!b[n]&&map[n][k]!=0&&vis[C[n]][C[k]]==0){
if(dis[n]==-1)dis[n]=dis[k]+map[n][k];
else dis[n]=min(dis[n],dis[k]+map[n][k]);
}
}
cout<<dis[T];
return 0;
}