#include <iostream>
#include <cstdio>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <stdlib.h>
#include <functional>
#include <string>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#include <nmmintrin.h>
#include <chrono>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#ifdef _MSC_VER
#define __builtin_popcount _mm_popcnt_u32
#define __builtin_popcountll _mm_popcnt_u64
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a%b);
}
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T &val) {
	std::fill((T*)array, (T*)(array + N), val);
}


struct UnionFind{
	vector<int> data;
	int n, m;

	UnionFind(int ns, int ms) : n(ns), m(ms), data(ns + ms, INT_MIN){
		REP(i, m){
			data[i + n] = -i-1;
		}
	}

	bool unite(int a, int b){//矛盾の判定
		a = root(a), b = root(b);

		if (a == b)
			return true;

		if (data[a] != INT_MIN && data[b] != INT_MIN){
			if (data[a] != data[b]){
				return false;
			}
			else {
				if (rand() % 2)
					data[b] = a;
				else
					data[a] = b;
			}
		}
		else if (data[a] != INT_MIN)
			data[b] = a;
		else if (data[b] != INT_MIN)
			data[a] = b;
		else{
			if (rand() % 2)
				data[b] = a;
			else
				data[a] = b;
		}
		return true;
	}


	bool unitebu(int a, int b){//矛盾の判定 aをbの部活
		a = root(a);
		b += n;

		if (data[a] == data[b])
			return true;
		if (data[a] != INT_MIN)
			return false;

		data[a] = b;
		return true;
	}

	bool findset(int a, int b){
		return root(a) == root(b);
	}

	int root(int x){
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
};

signed main() {
	int n, m, k;
	cin >> n >> m >> k;
	UnionFind uf(n,m);
	REP(i, k){
		int t, a, b;
		scanf("%d %d %d", &t, &a, &b);
		bool ret = 1;
		a--, b--;
		if (t == 1){
			ret = uf.unite(a, b);
		}
		else if (t == 2){
			ret = uf.unitebu(a, b);
		}

		if (!ret){
			printf("%d\n", i + 1);
			return 0;
		}
	}

	printf("0\n");

	return 0;
}