#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstdio>
using namespace std;
#define F first
#define S second
typedef long long ll;
typedef pair<ll,ll> P;
int n,m,k;
int p[100005],c[100005];
int find(int x){
    if(p[x]==x)return x;
    else return p[x]=find(p[x]);
}
int main(void){
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)p[i]=i;
    for(int i=1;i<=k;i++){
        int x,a,b;
        cin>>x>>a>>b;
        if(x==1){
            a=find(a);
            b=find(b);
            if(c[a]==0&&c[b]==0)p[a]=b;
            else if(c[a]!=0&&c[b]==0)p[b]=a;
            else if(c[a]==0&&c[b]!=0)p[a]=b;
            else if(c[a]==c[b])p[a]=b;
            else{
                cout<<i<<endl;
                return 0;
            }
        }else{
            a=find(a);
            if(c[a]==0)c[a]=b;
            else if(c[a]!=b){
                cout<<i<<endl;
                return 0;
            }
        }
    }
    cout<<0<<endl;
    return 0;
}
