
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef queue<int> QI;

//ifstream in("all.in");
#define in cin
int n1, n2;

int dostep(QI &q1, QI &q2, VVI &a, VI &num)
{
	if (q1.empty())
		return 0;
	while (!q1.empty())
	{
		int act = q1.front();
		q1.pop();
		for (int i=0; i<(int)a[act].size(); i++)
			if (--num[a[act][i]] == 0)
				if (act < n1 && a[act][i] < n1 || act >= n1 && a[act][i] >= n1)
					q1.push(a[act][i]);
				else
					q2.push(a[act][i]);
	}
	return 1;
}

int main()
{
	int d, n;
	while ((in >> n1 >> n2 >> d) && (n1 || n2 || d))
	{
		n = n1 + n2;
		VVI a(n);
		VI num_keep(n, 0);
		for (int i=0; i<d; i++)
		{
			int x, y;
			in >> x >> y;
			x--;
			y--;
			a[y].push_back(x);
			num_keep[x]++;
		}
		int sol = n + 1;
		for (int start=0; start<2; start++)
		{
			VI num = num_keep;
			QI q[2];
			for (int i=0; i<n; i++)
				if (num[i] == 0)
					if (i < n1)
						q[0].push(i);
					else
						q[1].push(i);
			int act = 1;
			while (1)
			{
				int old = act;
				act += dostep(q[start], q[(start+1)%2], a, num);
				act += dostep(q[(start+1)%2], q[start], a, num);
				if (act == old)
					break;
			}
			if (act < sol)
				sol = act;
		}
		cout << sol << endl;
	}
	return 0;
}
