#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
	int children;
	int origchildren;
	int disk;
	int rdep;
};

struct node nodes[100000];
int N1, N2, N;

struct dep {
	int parent, child;
};

struct dep deps[100000];
int D;

int comp (const void *aa, const void *bb)
{
	dep *a = (dep*)aa;
	dep *b = (dep*)bb;
	if (a->child < b->child)
		return -1;
	if (a->child > b->child)
		return 1;
	return 0;
}

struct queue {
	int q[200000];
	int s, e;
};

struct queue dq[2];

void enqueue (struct queue * q, int i)
{
	/* printf ("DEBUG: Enqueue %d in %d\n", i, q-dq); */
	q->q[q->e++] = i;
}

int dequeue (struct queue * q)
{
	if (q->s == q->e)
		return -1;
	/* printf ("DEBUG: Deque %d from %d\n", q->q[q->s], q-dq); */
	return q->q[q->s++];
}

void init (struct queue * q)
{
	q->s = q->e = 0;
}

int empty (struct queue * q)
{
	return (q->s == q->e);
}

int main ()
{
	int start, i, j, count[2];
	FILE * in = stdin;
	while (1) {
		fscanf (in, "%d %d %d", &N1, &N2, &D);
		N = N1 + N2;
		for (i=0; i<N1; ++i) {
			nodes[i].children = 0;
			nodes[i].origchildren = 0;
			nodes[i].disk = 0;
			nodes[i].rdep = -1;
		}
		for (; i<N; ++i) {
			nodes[i].children = 0;
			nodes[i].origchildren = 0;
			nodes[i].disk = 1;
			nodes[i].rdep = -1;
		}
		if (!N1 && !N2 && !D)
			break;
		for (i=0; i<D; ++i) {
			fscanf (in, "%d %d", &deps[i].parent, &deps[i].child);
			deps[i].parent--; deps[i].child--;
			nodes[deps[i].parent].children++;
			nodes[deps[i].parent].origchildren++;
		}
		qsort (deps, D, sizeof (struct dep), comp);
		for (i=0; i<D; ++i) {
			if (nodes[deps[i].child].rdep < 0)
				nodes[deps[i].child].rdep = i;
		}
		count[0] = count[1] = 0;
		for (j=0; j<2; ++j) {
		/* printf ("DEBUG: Loopcount = %d\n", j); */
		init (dq); init (dq+1);
		start = j;
		for (i=0; i<N; ++i) {
			if (nodes[i].children == 0) {
				enqueue (dq+nodes[i].disk, i);
			}
		}
		while (!empty (dq) || !empty (dq+1)) {
			int ret;
			while ((ret = dequeue (dq+start)) >= 0) {
				int i = nodes[ret].rdep;
				/* printf ("DEBUG: node %d rdep %d\n", ret, i); */
				if (i < 0)
					continue;
				for (; (i <= D) && (deps[i].child == ret);i++) {
					struct node *p = &nodes[deps[i].parent];
					/* printf ("DEBUG: node %d children %d\n", deps[i].parent, p->children); */
					if (!--p->children) {
						enqueue (dq+p->disk,
							 deps[i].parent);
					}
				}
			}
			count[j] ++;
			start = 1-start;
		}
		for (i=0; i<N; ++i) {
			nodes[i].children = nodes[i].origchildren;
		}
		}
		if (count[0] < count[1])
			printf ("%d\n", count[0]+1);
		else
			printf ("%d\n", count[1]+1);
	}
	return 0;
}
