#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
#include <set>

using namespace std;

enum {INF = -10};

int main() {
	int n, m, k;
	cin >> n >> m;
	cin >> k;
	int r, c;
	vector <vector <int> > f(n + 2);
	for (int i = 0; i <= n + 1; ++i)
		f[i].resize(m + 2, -1);
	for (int i = 0; i <= n + 1; ++i) {
		f[i][0] = INF;
		f[i][m + 1] = INF;
	}
	for (int i = 0; i <= m + 1; ++i) {
		f[0][i] = INF;
		f[n + 1][i] = INF;
	}
	queue <int> rs;
	queue <int> cs;
	for (int i = 1; i <= k; ++i) {
		cin >> r >> c;
		rs.push(r);
		cs.push(c);
		f[r][c] = 0;
	}
	int dr[] = { 1, -1, 0, 0 };
	int dc[] = { 0, 0, 1, -1 };
	while (!rs.empty()) {
		r = rs.front();
		c = cs.front();
		rs.pop();
		cs.pop();
		for (int j = 0; j < 4; ++j) {
			int nr = r + dr[j];
			int nc = c + dc[j];
			if (f[nr][nc] == -1) {
				f[nr][nc] = f[r][c] + 1;
				rs.push(nr);
				cs.push(nc);
			}
		}
	}
	int left = 0;
	int right = n + m;
	while (left < right) {
		set <pair <int, int>> used;
		int med = (left + right + 1) / 2;
		bool good = false;
		for (int i = 1; i <= n; ++i) {
			if (f[i][1] >= med) {
				rs.push(i);
				cs.push(1);
				used.insert(make_pair(i, 1));
			}
		}
		while (!rs.empty()) {
			r = rs.front();
			c = cs.front();
			rs.pop();
			cs.pop();
			for (int j = 0; j < 4; ++j) {
				int nr = r + dr[j];
				int nc = c + dc[j];
				if (f[nr][nc] >= med && used.find(make_pair(nr, nc)) == used.end()) {
					used.insert(make_pair(nr, nc));
					rs.push(nr);
					cs.push(nc);
					if (nc == m)
						good = true;
				}
			}
		}
		if (!good) {
			right = med - 1;
		}
		else {
			left = med;
		}
	}
	cout << left;
	return 0;
}