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

typedef long long ll;
typedef pair<int, int> 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 1e9+7

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

int s[100001], v[100001], r[100001];

void init() {
  rep(i, 0, 100001) {
    s[i] = i;
    r[i] = 0;
  }
}

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

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if(x == y) return;

  if(r[x] < r[y]) {
    s[x] = y;
    if(v[x]) v[y] = v[x];
  }else {
    s[y] = x;
    if(r[x] == r[y]) r[x]++;
    if(v[y]) v[x] = v[y];
  }
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[200001], b[200001], c[200001];
  init();
  rep(i, 0, k) cin >> a[i] >> b[i] >> c[i];
  rep(i, 0, k) {
    if(a[i] == 1) {
      int bb = find(b[i]);
      int cc = find(c[i]);
      if(v[bb] && v[cc] && v[bb] != v[cc]) {
        cout << i + 1 << endl;
        return 0;
      }
      unite(bb, cc);
    }else {
      int bb = find(b[i]);
      if(v[bb] && v[bb] != c[i]) {
        cout << i + 1 << endl;
        return 0;
      }
      v[bb] = c[i];
    }
  }

  cout << 0 << endl;
}