Akira, the student council president of A High School, decided to investigate which club activities the A High School students belong to. A High School has N students numbered 1 to N and M types of club activities numbered 1 to M. There is no limit to the number of people in each club activity, and there can be 0 club activities. However, according to the school rules of A High School, students can only belong to one club activity. All students follow this school rule.

Akira asked student members to investigate and obtained records for line K, such as each line being one of the following:

* Student a and student b belong to the same club activities.
* Student c belongs to club activity x.



However, there may be a contradiction in this record that would cause someone to violate school rules. Akira decided to look at the first line in order and look for the first line that could be judged to be inconsistent.

Create a program that finds the number of the first line that can be determined to be inconsistent given a record of K lines.



Input

The input is given in the following format.


NMK
record1
record2
::
recordK


The first line gives the number of students N (1 ≤ N ≤ 100000), the number of club activity types M (1 ≤ M ≤ 100000), and the number of recorded lines K (1 ≤ K ≤ 200000). Each line of record, recordi, is given in the following K lines in one of the following formats:


1 a b


Or


2 c x


When the first digit is "1", it indicates that student a (1 ≤ a ≤ N) and student b (1 ≤ b ≤ N) belong to the same club activity. However, a ≠ b.

When the first digit is "2", it indicates that student c (1 ≤ c ≤ N) belongs to club activity x (1 ≤ x ≤ M).

Output

Output the number of the first line that can be determined to be inconsistent on one line. If not found, output 0 on one line.

Examples

Input

3 2 5
1 1 2
1 2 3
2 1 1
2 3 2
2 2 1


Output

4


Input

3 2 4
1 1 2
2 1 1
2 3 2
2 2 1


Output

0


Input

3 2 2
1 1 2
2 1 1


Output

0
/*
このコード、と～おれ!
Be accepted!
∧＿∧　
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
		  　　　　　　　　　　(¸.·´ (¸.·'* ☆
*/

#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>
#include <functional>
#include <complex>
#include <list>
#include <cassert>
#include <iomanip>
#include <set>
#include <stack>
#include <bitset>

////多倍長整数, cpp_intで宣言
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;

//#pragma gcc target ("avx2")
//#pragma gcc optimization ("Ofast")
//#pragma gcc optimization ("unroll-loops")
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, n) for(int i = 1; i <= (n); ++i)
#define rep2(i, n) for(int i = 2; i < (n); ++i)
#define repr(i, n) for(int i = n; i >= 0; --i)
#define reprm(i, n) for(int i = n - 1; i >= 0; --i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a) //少数出力
#define printLdb(a) printf("%.50Lf\n", a) //少数出力
#define printdbd(a) printf("%.16lf\n", a) //少数出力(桁少なめ)
#define prints(s) printf("%s\n", s.c_str()) //string出力
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI)
#define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d)) /*(a, b) から (c, d) のマンハッタン距離 */
#define inf numeric_limits<double>::infinity();
#define linf numeric_limits<long double>::infinity()

using ll = long long;
using ull = unsigned long long;

constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);

using namespace std;

void scans(string & str) {
	char c;
	str = "";
	scanf("%c", &c);
	if (c == '\n')scanf("%c", &c);
	while (c != '\n' && c != -1 && c != ' ') {
		str += c;
		scanf("%c", &c);
	}
}

void scanc(char& str) {
	char c;
	scanf("%c", &c);
	if (c == -1)return;
	while (c == '\n') {
		scanf("%c", &c);
	}
	str = c;
}

double acot(double x) {
	return PI / 2 - atan(x);
}

ll LSB(ll n) { return (n & (-n)); }

/*-----------------------------------------ここからコード-----------------------------------------*/

/*
* @title unionfind
* @docs kyopro/docs/unionfind.md
*/

//0-indexed
struct unionfind {
	vector<ll> par, siz;

	unionfind(ll n) : par(n), siz(n) {
		for (ll i = 0; i < n; ++i) {
			//全部根で初期化
			par[i] = i;
			//サイズは1
			siz[i] = 1;
		}
	}

	void init(ll n) {
		par.resize(n);
		siz.resize(n);
		for (ll i = 0; i < n; ++i) {
			//全部根で初期化
			par[i] = i;
			//サイズは1
			siz[i] = 1;
		}
	}

	//根を返す
	ll find(ll a) {
		return par[a] == a ? a : par[a] = find(par[a]);
	}

	//くっつける。元から同じだったらfalseを返す
	bool unite(ll a, ll b) {
		ll x = find(a), y = find(b);
		if (x == y)return false;
		else if (siz[x] < siz[y]) {
			par[x] = y;
			siz[y] += siz[x];
		}
		else if (siz[x] > siz[y]) {
			par[y] = x;
			siz[x] += siz[y];
		}
		else {
			par[x] = y;
			siz[y] += siz[x];
		}
		return true;
	}

	//同じ集合か判定する
	bool same(ll a, ll b) {
		return find(a) == find(b);
	}

	//サイズを返す
	ll size(ll a) {
		return siz[find(a)];
	}

	//同じ集合に属す葉を纏めて返す
	vector<ll> leaf(ll a) {
		vector<ll> x;
		ll n = par.size();
		for (ll i = 0; i < n; ++i)if (same(a, i))x.push_back(i);
		return x;
	}

};


int main() {

	int n, m, k;
	scanf("%d%d%d", &n, &m, &k);
	unionfind uni(n);
	vector<int> club(n);
	rep(query, k) {
		int type, a, b;
		scanf("%d%d%d", &type, &a, &b);
		if (type == 1) {
			--a, --b;
			if (club[uni.find(a)] and club[uni.find(b)] and club[uni.find(a)] != club[uni.find(b)]) {
				printf("%d\n", query + 1);
				return 0;
			}
			else {
				club[uni.find(a)] = club[uni.find(b)] = max(club[uni.find(a)], club[uni.find(b)]);
				uni.unite(a, b);
			}
		}
		else {
			--a;
			if (club[uni.find(a)] and club[uni.find(a)] != b) {
				printf("%d\n", query + 1);
				return 0;
			}
			else {
				club[uni.find(a)] = b;
			}
		}
	}
	puts("0");

	Please AC;
}
