#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
const int oo=1000000000;
int f[2][201][201];
int w[201][201];
int a[1001];
int n,l;
int main()
{
freopen("service.in","r",stdin);
freopen("service.out","w",stdout);
scanf("%d%d",&n,&l);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
scanf("%d",&w[i][j]);
}
for (int i=1;i<=l;i++) scanf("%d",&a[i]);
a[0]=3;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
f[0][i][j]=oo;
f[1][i][j]=oo;
}
f[0][1][2]=0;f[0][2][1]=0;
int t;
for (int k=1;k<=l;k++)
{
t=k%2;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
f[t][i][j]=oo;
}
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (i!=j && i!=a[k-1] && j!=a[k-1])
{
if (f[t][i][j]>f[1-t][i][j]+w[a[k-1]][a[k]]) f[t][i][j]=f[1-t][i][j]+w[a[k-1]][a[k]];
if (f[t][i][a[k-1]]>f[1-t][i][j]+w[j][a[k]]) f[t][i][a[k-1]]=f[1-t][i][j]+w[j][a[k]];
if (f[t][a[k-1]][j]>f[1-t][i][j]+w[i][a[k]]) f[t][a[k-1]][j]=f[1-t][i][j]+w[i][a[k]];
if (f[t][j][i]>f[1-t][i][j]+w[a[k-1]][a[k]]) f[t][j][i]=f[1-t][i][j]+w[a[k-1]][a[k]];
if (f[t][j][a[k-1]]>f[1-t][i][j]+w[i][a[k]]) f[t][j][a[k-1]]=f[1-t][i][j]+w[i][a[k]];
if (f[t][a[k-1]][i]>f[1-t][i][j]+w[j][a[k]]) f[t][a[k-1]][i]=f[1-t][i][j]+w[j][a[k]];
}
}
int ans=oo;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (i!=j && i!=a[l] && j!=a[l])
{
if (ans>f[l%2][i][j]) ans=f[l%2][i][j];
}
printf("%d\n",ans);
return 0;
}