# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
using namespace std;
using LL = long long;
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
const double PI = acos(-1);

int Par[100001];
int Rank[100001];
int id[100001];

bool flag;

void init(int n) {
	for (int i = 0; i < n; i++) {
		Par[i] = i;
		Rank[i] = 0;
		id[i] = -1;
	}
}

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 (Rank[x]>Rank[y])swap(x, y);
	Par[x] = y;

	if (id[x] != -1 && id[y] != -1 && id[x] != id[y])flag = true;

	if (id[x] != -1) {
		id[y] = id[x];
	}
}

bool same(int x, int y) {
	return find(x) == find(y);
}


int main() {
	int n, m, k;
	cin >> n >> m >> k;
	init(n);
	int a, b, c;
	for (int i = 0; i < k; i++) {
		cin >> a >> b >> c;
		if (a == 1) {
			unite(b, c);
			if (flag) {
				cout << i + 1 << endl;
				return 0;
			}
		}
		else if (a == 2) {
			b = find(b);
			if (id[b] != -1 && id[b] != c) {
				cout << i + 1 << endl;
				return 0;
			}
			id[b] = c;
		}
	}
	cout << 0 << endl;
	return 0;
}