N, K = map(int, input().split()) S1 = set(i+1 for i in range(4)) S2 = set() Ans = '-1' if N >= K: for _ in range(N-1): a, b = map(int, input().split()) S2 |= {a} S2 |= {b} if len(S1 - set(S2)) == 0: Ans = K-1 print(Ans)