#include<bits/stdc++.h>
using namespace std;
pair<int,int>p;
vector<pair<int,int> >v[90002];
int n,m,pi;
int fr,ix,iy;
int a[303][303];
int dis[303][303];
int bfs(int ii,int jj){
for(int i=0;i<=300;i++)for(int j=0;j<=300;j++)dis[i][j]=100000000;
queue<pair<int,int> >Q;
if(a[ii][jj]!=1){
for(int i=0;i<v[0].size();i++){
p=v[0][i];
int x=p.first;
int y=p.second;
int ds=abs(ii-x) + abs(y-jj);
dis[x][y]=ds;
//cout<<dis[x][y]<<" "<<x<<" "<<y<<endl;
Q.push(make_pair(x,y));
}
}
else{
Q.push(make_pair(ii,jj));
dis[1][1]=0;
}
while(!Q.empty()){
p=Q.front();
Q.pop();
int x=p.first,y=p.second;
//printf("%d %d dis=%d\n",x,y,dis[x][y]);
int ch=a[x][y];
for(int i=0;i<v[ch].size();i++){
p=v[ch][i];
int xx=p.first,yy=p.second;
int ds=abs(x-xx)+abs(y-yy);
//printf("%d %d %d\n",dis[xx][yy],xx,yy);
if(dis[x][y]+ds < dis[xx][yy]){
dis[xx][yy]=dis[x][y]+ds;
Q.push(make_pair(xx,yy));
}
}
}
return dis[ix][iy];
}
int main(void)
{
int n,m,pi;
scanf("%d%d%d",&n,&m,&pi);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int x;
scanf("%d",&x);
a[i][j]=x;
v[x-1].push_back(make_pair(i,j));
if(x==pi)ix=i,iy=j;
}
}
int ans=bfs(1,1);
printf("%d\n",ans);
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnBhaXI8aW50LGludD5wOwp2ZWN0b3I8cGFpcjxpbnQsaW50PiA+dls5MDAwMl07CmludCBuLG0scGk7CmludCBmcixpeCxpeTsKaW50IGFbMzAzXVszMDNdOwppbnQgZGlzWzMwM11bMzAzXTsKaW50IGJmcyhpbnQgaWksaW50IGpqKXsKICAgIGZvcihpbnQgaT0wO2k8PTMwMDtpKyspZm9yKGludCBqPTA7ajw9MzAwO2orKylkaXNbaV1bal09MTAwMDAwMDAwOwogICAgcXVldWU8cGFpcjxpbnQsaW50PiA+UTsKICAgIGlmKGFbaWldW2pqXSE9MSl7CiAgICAgICAgZm9yKGludCBpPTA7aTx2WzBdLnNpemUoKTtpKyspewogICAgICAgICAgICBwPXZbMF1baV07CiAgICAgICAgICAgIGludCB4PXAuZmlyc3Q7CiAgICAgICAgICAgIGludCB5PXAuc2Vjb25kOwogICAgICAgICAgICBpbnQgZHM9YWJzKGlpLXgpICsgYWJzKHktamopOwogICAgICAgICAgICBkaXNbeF1beV09ZHM7CiAgICAgICAgICAgIC8vY291dDw8ZGlzW3hdW3ldPDwiICI8PHg8PCIgIjw8eTw8ZW5kbDsKICAgICAgICAgICAgUS5wdXNoKG1ha2VfcGFpcih4LHkpKTsKICAgICAgICB9CiAgICB9CiAgICBlbHNlewogICAgICAgICBRLnB1c2gobWFrZV9wYWlyKGlpLGpqKSk7CiAgICAgICAgIGRpc1sxXVsxXT0wOwogICAgfQogICAgd2hpbGUoIVEuZW1wdHkoKSl7CiAgICAgICAgcD1RLmZyb250KCk7CiAgICAgICAgUS5wb3AoKTsKICAgICAgICBpbnQgeD1wLmZpcnN0LHk9cC5zZWNvbmQ7CiAgICAgICAgLy9wcmludGYoIiVkICVkIGRpcz0lZFxuIix4LHksZGlzW3hdW3ldKTsKICAgICAgICBpbnQgY2g9YVt4XVt5XTsKICAgICAgICBmb3IoaW50IGk9MDtpPHZbY2hdLnNpemUoKTtpKyspewogICAgICAgICAgICBwPXZbY2hdW2ldOwogICAgICAgICAgICBpbnQgeHg9cC5maXJzdCx5eT1wLnNlY29uZDsKICAgICAgICAgICAgaW50IGRzPWFicyh4LXh4KSthYnMoeS15eSk7CiAgICAgICAgICAgIC8vcHJpbnRmKCIlZCAlZCAlZFxuIixkaXNbeHhdW3l5XSx4eCx5eSk7CiAgICAgICAgICAgIGlmKGRpc1t4XVt5XStkcyA8IGRpc1t4eF1beXldKXsKICAgICAgICAgICAgICAgIGRpc1t4eF1beXldPWRpc1t4XVt5XStkczsKCiAgICAgICAgICAgICAgICBRLnB1c2gobWFrZV9wYWlyKHh4LHl5KSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gZGlzW2l4XVtpeV07Cn0KaW50IG1haW4odm9pZCkKewogICAgaW50IG4sbSxwaTsKICAgIHNjYW5mKCIlZCVkJWQiLCZuLCZtLCZwaSk7CiAgICBmb3IoaW50IGk9MTtpPD1uO2krKyl7CiAgICAgICAgZm9yKGludCBqPTE7ajw9bTtqKyspewogICAgICAgICAgICBpbnQgeDsKICAgICAgICAgICAgc2NhbmYoIiVkIiwmeCk7CiAgICAgICAgICAgIGFbaV1bal09eDsKICAgICAgICAgICAgdlt4LTFdLnB1c2hfYmFjayhtYWtlX3BhaXIoaSxqKSk7CiAgICAgICAgICAgIGlmKHg9PXBpKWl4PWksaXk9ajsKICAgICAgICB9CiAgICB9CiAgICBpbnQgYW5zPWJmcygxLDEpOwogICAgcHJpbnRmKCIlZFxuIixhbnMpOwoKICAgIHJldHVybiAwOwp9Cg==