#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 110
#define max(a,b) a>b?a:b
int g[MAX][MAX],vis[MAX],link[MAX];
int n,m,k,temp;
int dfs(int x){
for(int i =1;i <= temp;i++){
if(g[x][i] && vis[i] == 0){
vis[i] = 1;
if(link[i] == -1 || dfs(link[i])){
link[i] = x;
return 1;
}
}
}
return 0;
}
int main(){
int num,a,b;
while(~scanf("%d",&n),n){
scanf("%d%d",&m,&k);
temp = max(n,m);
memset(g,0,sizeof(g));
memset(link,-1,sizeof(link));
for(int i = 1;i <= k;i++){
scanf("%d%d%d",&num,&a,&b);
g[a][b] = 1;
}
int ans = 0;
for(int i = 1;i <= temp;i++){
memset(vis,0,sizeof(vis));
if(dfs(i)) ans++;
}
cout<<ans<<endl;
}
return 0;
}
09-08
09-08
09-08