#include<iostream>
using namespace std;
class human{
public:
    int parent;
    int club;
    human():parent(-1),club(-1){}
};
human *line;
bool merge(int ,int);
bool set(int,int);
int getparent(int);
int main(){
    int N,M,K;
    cin>>N>>M>>K;
    line=new human[N+1];
    int i;
    for(i=0;i<K;++i){
        int a,b,c;
        cin>>a>>b>>c;
        if(a==1){
            if(merge(b,c)){
                break;
            }
        }else{
            if(set(b,c)){
                break;
            }
        }
    }
    cout<<(i==K?0:i+1)<<endl;
    return 0;
}
bool set(int num,int club){
    num=getparent(num);
    if(line[num].club!=-1){
        if(line[num].club!=club){
            return true;
        }
        return false;
    }
    line[num].club=club;
    return false;
}

bool merge(int A,int B){
    A=getparent(A);
    B=getparent(B);
    if(A==B)return false;
    if(line[A].club!=-1&&line[B].club!=-1){
        if(line[A].club!=line[B].club){
            return true;
        }else{
            line[B].parent=A;
        }
    }else{
        if(line[A].club!=-1){
            line[B].parent=A;
        }else{
            line[A].parent=B;
        }
    }
    return false;
}
int getparent(int a){
    if(line[a].parent==-1){
        return a;
    }else{
        line[a].parent = getparent(line[a].parent);
        return line[a].parent;
    }
}