#include<bits/stdc++.h>
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll INF=1000000010;
const ll MOD=1000000007;
const ll LINF=4000000000000000000;
const long double EPS=1e-9;
typedef pair<int,int> P;
struct UnionFind{
    vector<int> par;
    vector<int> dep;
    
    UnionFind(int siz){
        par.assign(siz,0);
        dep.assign(siz,0);
        for(int i=0;i<siz;i++){
            par[i]=i;
        }
    };

    int find(int x){
        if(par[x]==x){
            return x;
        }else{
            return par[x]=find(par[x]);
        }
    }

    void unite(int x,int y){
        x=find(x);
        y=find(y);
        if(x==y){
            return;
        }
        if(dep[x]<dep[y]){
            par[x]=y;
        }else{
            par[y]=x;
            if(dep[x]==dep[y]){
                dep[x]++;
            }
        }
    }

    bool same(int x,int y){
        return find(x)==find(y);
    }
};
int s[100010];
int main(){
    memset(s,-1,sizeof(s));
    int n,m,k;cin>>n>>m>>k;
    UnionFind uf(n+1);
    int ans=0;
    for(int i=1;i<=k;i++){
        int t,a,b;cin>>t>>a>>b;
        if(t==1){
            if(s[uf.find(a)]>0){
                int d=s[uf.find(a)];
                if(s[uf.find(b)]==-1){
                    uf.unite(a,b);
                    s[uf.find(a)]=d;
                }else if(s[uf.find(b)]==s[uf.find(a)]){
                    uf.unite(a,b);
                    s[uf.find(a)]=d;
                }else{
                    if(ans==0)ans=i;
                }
            }else{
                if(s[uf.find(b)]==-1){
                    uf.unite(a,b);
                    s[uf.find(a)]=-1;
                }else{
                    int d=s[uf.find(b)];
                    uf.unite(a,b);
                    s[uf.find(a)]=d;
                }
            }
        }else{
            if(s[uf.find(a)]>0){
                    if(s[uf.find(a)]!=b){
                        if(ans==0)ans=i;
                    }
                }else{
                    s[uf.find(a)]=b;
                }
        }
    }
    cout<<ans<<endl;
}   
