//disjoin-set
#include<bits/stdc++.h>
using namespace std;

class disjoinset {
	public:
		vector<int> rank,p;
	disjoinset (){}
	disjoinset(int size){
	rank.resize(size,0);
	p.resize(size,0);
	for(int i=0; i<size;i++)makeSet(i);
	}
	void makeSet(int x){
	p[x] = x;
	rank[x] =0;
	}
	bool same(int x,int y){
	return findSet(x) == findSet(y);
	}
	void unite(int x,int y){
		link(findSet(x),findSet(y));
	}
	void link(int x,int y){
		if(rank[x] > rank[y]){
		 p[y] = x;
		}else{
		 p[x] = y;
		 if(rank[x]==rank[y] )rank[y]++;
		}
	}
	int findSet(int x){
		if(x!=p[x])p[x] = findSet(p[x]);
		return p[x];
	}

};
 
int main(){
	int n,m,k;
	cin>>n>>m>>k;
	disjoinset ds  = disjoinset(n);
	vector<int> a(n,0);
	bool flag = true;
	for(int i=0;i<k;i++){
		int t;
		cin>>t;
		if(!flag){
			int b,c;
			cin>>b>>c;
			continue;
		}
		if(t==1) {
			int b,c;
			cin>>b>>c;
			b--,c--;
			int bb = a[ds.findSet(b)] ;
			int cc = a[ds.findSet(c)] ;
			if( bb==0 || cc==0 ||bb==cc){
				ds.unite(b,c);
				a[ds.findSet(b)] = max(bb,cc);
			}else{
				cout<<i+1<<endl;
				flag = false;
			}
		}else if(t==2){
			int c,x;
			cin>>c>>x;
			c--;
			int root = ds.findSet(c);
			if( a[root] == x|| a[root] == 0){
					a[root] = x ;
			}else{
				cout<<i+1<<endl;
				flag = false;
			}
		}
	}
	if(flag)cout<<0<<endl;

	return 0;
}

