import java.io.*;
import java.util.*;
import java.math.*;

public class maxpiece_aa {
	public static void main(String[] args) {
		new maxpiece_aa().run();
	}

	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	boolean eof = false;

	public void run() {
		Locale.setDefault(Locale.US);
		try {
			br = new BufferedReader(new FileReader(FNAME + ".in"));
			out = new PrintWriter(FNAME + ".out");
			solve();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			System.exit(566);
		}
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return "0";
			}
		}
		return st.nextToken();
	}

	int nextInt() {
		return Integer.parseInt(nextToken());
	}

	void myAssert(boolean u, String message) {
		if (!u) {
			throw new Error("Assertion failed!!! " + message);
		}
	}

	int inBounds(int x, int l, int r, String name) {
		myAssert(l <= x && x <= r, name + " is not in bounds: " + x
				+ " not in [" + l + ", " + r + "]");
		return x;
	}

	String FNAME = "maxpiece";

	private void solve() throws IOException {
		int n = inBounds(nextInt(), 1, 1000000000, "n");
		int m = inBounds(nextInt(), 1, 1000000000, "m");
		int k = inBounds(nextInt(), 0, 100000, "k");
		int[][] c = new int[2][k + 2];
		int[] q = new int[2];
		for (int i = 0; i < k; i++) {
			int t = nextInt();
			c[t][q[t]++] = nextInt();
		}
		c[0][q[0]++] = 0;
		c[0][q[0]++] = n;
		c[1][q[1]++] = 0;
		c[1][q[1]++] = m;
		for (int i = 0; i < c.length; i++) {
			Arrays.sort(c[i], 0, q[i]);
		}
		int[] max = new int[2];
		for (int i = 0; i < q.length; i++) {
			for (int j = 1; j < c[i].length; j++) {
				max[i] = Math.max(max[i], c[i][j] - c[i][j - 1]);
			}
		}
		out.println(Math.min(max[0], max[1]));
	}
}
