'''
拓展域并查集验证矛盾
S(i)表示前i个位置中1的个数
并查集状态表示
x 为正数或者0表示 S(x)为偶数这个命题
x 为负数表示 S(x)为奇数这个命题
并查集同一个子集中的两个节点表示两个命题一定同时为真或者同时为假,也就是等价
这样的关系具备传递性,所以可以用并查集维护
如果a b两个命题等价,又有新条件说 a和非b等价,就可以判断出矛盾
'''
import sys
class MergeSetExt:
def __init__(self, max_key_val = 0, trans_key_func=None):
if trans_key_func is not None and max_key_val > 0:
# 如果能够提供转换key的回调,并查集内部用线性表存储
self.trans_key_callback = trans_key_func
self.m = [-1 for _ in range(max_key_val+1)]
self.__root2cluster_size = [0 for _ in range(max_key_val+1)]
else:
# 如果不能提供转换key的回调,并查集内部用hash表存储
self.trans_key_callback = None
self.m = {}
self.__root2cluster_size = {}
self.__root_cnt = 0
def getRoot(self, node):
buf = []
root = self.trans_key_callback(node) if self.trans_key_callback else node
while self.m[root] != root:
buf.append(root)
root = self.m[root]
for key in buf:
self.m[key] = root
return root
def merge(self, a, b):
orig_a, orig_b = a, b
if self.trans_key_callback:
a, b = self.trans_key_callback(a), self.trans_key_callback(b)
for node in [a, b]:
if (self.trans_key_callback is None and node not in self.m) or (self.trans_key_callback is not None and self.m[node] == -1):
self.m[node] = node
self.__root2cluster_size[node] = 1
self.__root_cnt += 1
root1 = self.getRoot(orig_a)
root2 = self.getRoot(orig_b)
if root1 != root2:
self.m[root1] = root2
self.__root2cluster_size[root2] += self.__root2cluster_size[root1]
if self.trans_key_callback:
self.__root2cluster_size[root1] = 0
else:
self.__root2cluster_size.pop(root1)
self.__root_cnt -= 1
def isInSameSet(self, a, b):
if a == b:
return True
orig_a, orig_b = a, b
if self.trans_key_callback:
a, b = self.trans_key_callback(a), self.trans_key_callback(b)
for node in [a, b]:
if self.m[node] == -1:
return False
else:
for node in [orig_a, orig_b]:
if node not in self.m:
return False
return self.getRoot(orig_a) == self.getRoot(orig_b)
n = input()
m = int(input())
merge_set = MergeSetExt()
ans = -1
for i in range(1, m+1):
s = sys.stdin.readline()
a, b, w = s.split()
a, b = int(a), int(b)
if ans == -1:
if a-1 == 0:
if w == 'even':
if merge_set.isInSameSet(0, -b):
ans = i-1
else:
merge_set.merge(0, b)
else:
if merge_set.isInSameSet(0, b):
ans = i-1
else:
merge_set.merge(0, -b)
else:
if w == 'even':
if merge_set.isInSameSet(a-1, -b) or merge_set.isInSameSet(-(a-1), b):
ans = i-1
else:
merge_set.merge(a-1, b)
merge_set.merge(-(a-1), -b)
else:
if merge_set.isInSameSet(a-1, b) or merge_set.isInSameSet(-(a-1), -b):
ans = i-1
else:
merge_set.merge(a-1, -b)
merge_set.merge(-(a-1), b)
print(ans if ans != -1 else m)