//* Problem  : 1653 - All Discs Considered
//* Contest  : acm.tju.edu.cn
//* Type     : Solution
//* Date     : 2007.08.15
//* Author   : alt
//* Language : C++
//* Compiler : Microsoft Visual C++ 6.0

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>

#define MAX(a, b) ((a>=b)?a:b)
#define MIN(a, b) ((a<=b)?a:b)

struct si{
//struct stack_item{
	int x;
};

struct list{
	int id;
	list * p;
};

struct cd{
	int n, pre, ok;
	list * dep;
} a[100000],  at[100000];

int n1, n2, d;

void list_add(cd *root, int v)
{
	list * p = (list*)malloc(sizeof(list));
	p->id = v; p->p = root->dep;
	root->dep = p;
}

//***Stack  (Queue)
#define stack_size 100000				// sizeofstack

typedef si stack_item;				// type of stack elements

stack_item main_stack[stack_size];
stack_item tmp_stack[stack_size];
int i_main_stack, i_tmp_stack;

void stack_init()
{
	i_main_stack = i_tmp_stack = 0;
}

void stack_add(stack_item value) 
{
	tmp_stack[i_tmp_stack++] = value;
}

void stack_reverse()
{
	memcpy(main_stack, tmp_stack, (i_tmp_stack)*sizeof(stack_item));
	i_main_stack = i_tmp_stack;
	i_tmp_stack = 0;
}

int stack_getsize()
{
	return (i_main_stack);
}

stack_item stack_get(int k) 
{
	return main_stack[k];
}
//*** End Of Stack


void trace(int k)
{
	list *p = a[k].dep; si t; 
	while (p)
	{
		a[p->id].pre--;
		if (!a[p->id].ok && !a[p->id].pre) 
		{
			t.x = p->id;
			stack_add(t);
		}
		p = p->p;
	}
}

int try_cd(int k0)
{
	int i, k, res = 0, f = 1, count = 0, ccd, x; si t;
	stack_init();
	for (i = 0; i < n1 + n2; i++)
		if (!a[i].pre)
		{
			t.x = i;
			stack_add(t);
		}
	stack_reverse();
	///////////////////////////////////////////////////////
	ccd = k0;
	while (count < n1 + n2)
	{
		f = 1;
		while (f)
		{
			f = 0;
			k = stack_getsize();
			if (!k && (count < n1 + n2)) return 1000000000;
			for (i = 0; i < k; i++)
			{
				t = stack_get(i); x = t.x;
				if (a[x].n != ccd) stack_add(t);
				else 
				{
					a[x].ok = 1; count++;
					trace(x); f = 1;
				}
			}
			stack_reverse();
		}
		//if (count == n1 + n2) break;
		ccd = 1 - ccd;
		res++;
	}
	return res + 1;
}

int solve()
{
	int res1, res2;
	memcpy(at, a, sizeof(at));
	res1 = try_cd(0);
	memcpy(a, at, sizeof(a));
	res2 = try_cd(1);
	if (res1 < res2) return res1;
		else return res2;
}

int main()
{
	int i, res;
	while (scanf("%d %d %d", &n1, &n2, &d) == 3 && (n1 || n2 || d))
	{
		memset(a, 0, sizeof(a));
		int i, t1, t2;
		for (i = n1; i < n1 + n2; i++)
			a[i].n = 1;
		for (i = 0; i < d; i++)
		{
			scanf("%d %d", &t1, &t2);
			t1--; t2--;
			a[t1].pre++;
			list_add(&a[t2], t1);
		}
		res = solve();
		printf("%d\n", res);
	}
	return 0;
}