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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define mod (ll)(1e9+7)
#define int ll

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int s[100001];
int b[100001];

int find(int x) {
  return s[x] = s[x] == x ? x : find(s[x]);
}

bool unite(int x, int y) {
  x = find(x);
  y = find(y);
  if(x != y) {
    if(b[x] && b[y] && b[x] != b[y]) {
      return false;
    }
    if(b[x]) b[y] = b[x];
    s[x] = y;
  }
  return true;
}

bool bukatu(int x, int y) {
  if(b[find(x)] && b[find(x)] != y) {
    return false;
  }else {
    b[find(x)] = y;
    return true;
  }
}

int a[200001], c[200001], d[200001];

signed main() {
  int n, m, k;
  cin >> n >> m >> k;
  rep(i, 0, n + 1) s[i] = i;
  rep(i, 0, k) cin >> a[i] >> c[i] >> d[i];
  rep(i, 0, k) {
    if(a[i] == 1) {
      if(!unite(c[i], d[i])) {
        cout << i + 1 << endl;
        return 0;
      }
    }else {
      if(!bukatu(c[i], d[i])) {
        cout << i + 1 << endl;
        return 0;
      }
    }
  }
  cout << 0 << endl;
}

