K, N = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) def calc(): res = -10**18 for i in range(1, K+1): res = max(res, min(A[-i], B[-i])) return res for _ in range(3*K+1): A.append(calc()) try: print(A[N]) except: memo = [] for a in A[::-1]: if memo and memo[0]==a: break memo.append(a) m = len(memo) n = len(A) d = N+1 - n d %= m print(memo[::-1][d-1])