#include<iostream>
#include<cassert>
#include<map>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)

using namespace std;

#define MAX 100010
int par[MAX];

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

void unit(int x,int y) {
  x = find(x), y = find(y);
  if( x != y ) par[x] = y;
}

int main() {
  int N,M,K;
  cin >> N >> M >> K;
  rep(i,N+1) par[i] = i;
  int ans = -1;
  map<int,int> mp;
  rep(i,K) {
    int a,b,c;
    cin >> a >> b >> c;
    if( ans != -1 ) continue;
    if( a == 1 ) {
      if( mp.count(find(b)) && mp.count(find(c)) ) {
	if( mp[find(b)] != mp[find(c)] ) {
	  ans = i + 1;
	  continue;
	}
	unit(b,c);
      } else if( !mp.count(find(b)) && !mp.count(find(c)) ) {
	unit(b,c);
      } else {
	int id = ( mp.count(find(b)) ? mp[find(b)] : mp[find(c)]);
	unit(b,c);
	mp[find(b)] = id;
	mp[find(c)] = id;
	assert( find(b) == find(c));
      }
    } else {
      if( mp.count(find(b)) ) {
	if( mp[find(b)] != c ) {
	  ans = i + 1;
	  continue;
	}
      }
      mp[find(b)] = c;
    }
  }
  cout << ((ans==-1)?0:ans) << endl;
  return 0;
}