#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <time.h>
#include <random>
using namespace std;

mt19937 rnd(time(0)); //rnd()

struct point
{
	int x, y, z;
	point(int xx, int yy, int zz)
	{
		x = xx;
		y = yy;
		z = zz;
	}
	point()
	{

	}
};

int dat[65][65][65];
int vi;
vector <point> v;

int main()
{
	memset(dat, -1, sizeof(dat));

	string s = "put";

	
	/*
	int tn = 3, tm = 3, tk = 3, ti, tj, tl, tx, ty, tz;
	tn = tm = tk = 60;
	int kk = tn*tm*tk/2;
	ofstream gen("in" + s + ".txt");
	for (ti = 0; ti < kk; ti++)
	{
		tx = rnd() % tn + 1; ty = rnd() % tm + 1; tz = rnd() % tk + 1;
		dat[tx][ty][tz] = 0;
	}
	dat[1][1][1] = 0; dat[tn][tm][tk] = 0;
	gen << tn << " " << tm << " " << tk << "\n";
	for (tl = 1; tl <= tk; tl++)
	{
		for (tj = 1; tj <= tm; tj++)
		{
			for (ti = 1; ti <= tn; ti++)
			{
				if (dat[ti][tj][tl] == 0)
				{
					gen << 0;
				}
				else
				{
					gen << 1;
				}
				gen << " ";
			}
		}
	}

	gen.close();
	*/



	ifstream cin("in" + s + ".txt");
	ofstream cout("out" + s + ".txt");

	int n, m, k, i, j, l, step;

	cin >> n >> m >> k;
	for (l = 1; l <= k; l++)
	{
		for (j = 1; j <= m; j++)
		{
			for (i = 1; i <= n; i++)
			{
				cin >> dat[i][j][l];
				dat[i][j][l] *= -1;
			}
		}
	}

	dat[1][1][1] = 1;
	v.push_back(point(1, 1, 1));
	point t;

	while (vi != v.size())
	{
		t.x = v[vi].x; t.y = v[vi].y; t.z = v[vi].z; step = dat[t.x][t.y][t.z];
		vi++;

		if (dat[t.x-1][t.y][t.z] == 0)
		{
			dat[t.x-1][t.y][t.z] = step + 1;
			v.push_back(point(t.x-1, t.y, t.z));
		}
		if (dat[t.x+1][t.y][t.z] == 0)
		{
			dat[t.x+1][t.y][t.z] = step + 1;
			v.push_back(point(t.x+1, t.y, t.z));
		}
		if (dat[t.x][t.y-1][t.z] == 0)
		{
			dat[t.x][t.y-1][t.z] = step + 1;
			v.push_back(point(t.x, t.y-1, t.z));
		}
		if (dat[t.x][t.y+1][t.z] == 0)
		{
			dat[t.x][t.y+1][t.z] = step + 1;
			v.push_back(point(t.x, t.y+1, t.z));
		}
		if (dat[t.x][t.y][t.z-1] == 0)
		{
			dat[t.x][t.y][t.z-1] = step + 1;
			v.push_back(point(t.x, t.y, t.z-1));
		}
		if (dat[t.x][t.y][t.z+1] == 0)
		{
			dat[t.x][t.y][t.z+1] = step + 1;
			v.push_back(point(t.x, t.y, t.z+1));
		}
	}

	cout << dat[n][m][k] - 1;

	return 0;
}