#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
//英语 看博友分析 抄博友程序 拓扑排序 巧妙 图的数据结构 没掌握
int d[100010],dd[100010],head[100010];//图的数据结构 背
int cnt;//图的数据结构 背
int n1,n2;
struct nod{
int v;
int nex;
}edge[100010];//图的数据结构 背
queue<int> q[2];
void init()
{
cnt=0;
memset(head,-1,sizeof(head));//图的数据结构 背
memset(dd,0,sizeof(dd));
memset(d,0,sizeof(d));
}
void deal()
{
while(q[0].empty()!=1)
{
q[0].pop();
}
while(q[1].empty()!=1)
{
q[1].pop();
}
for(int i=1;i<=n1+n2;i++)
{
if(d[i]==0)
{
if(i<=n1)
{
q[0].push(i);
}else
{
q[1].push(i);
}
}
}
}
int fun(int k)
{
deal();
//cout<<"hello "<<k<<endl;
int ans=0;
while(q[k].empty()!=1 || q[k^1].empty()!=1)//抄博友程序 背
{
while(q[k].empty()!=1)
{
int u=q[k].front();
q[k].pop();
for(int i=head[u];i!=-1;i=edge[i].nex)
{
int v=edge[i].v;//抄博友程序 背
d[v]--;//抄博友程序 背
if(d[v]==0)
{
if(v<=n1)
{
q[0].push(v);
}else
{
q[1].push(v);
}
}
}
}
ans++;
k=k^1;
}
return ans;
}
int main()
{
int m;
while(scanf("%d%d%d",&n1,&n2,&m))
{
if(n1==0 && n2==0 && m==0)
{
break;
}
init();
int u,v;
for(int i=0;i<m;i++)
{
scanf("%d%d",&v,&u);
//the installation of package xi requires the previous installation of package yi.
edge[cnt].v=v;
edge[cnt].nex=head[u];
head[u]=cnt++;
d[v]++;//入度
dd[v]++;
}
int ans1=fun(0);
for(int i=1;i<=n1+n2;i++)
{
d[i]=dd[i];
}
int ans2=fun(1);
//cout<<ans1<<" "<<ans2<<endl;
int ans=min(ans1,ans2);
cout<<ans+1<<endl;//抄博友程序
}
return 0;
}