import sys from collections import deque def main(): K, N = map(int, sys.stdin.readline().split()) A = list(map(int, sys.stdin.readline().split())) B = list(map(int, sys.stdin.readline().split())) M = max(B) # Check if any initial term is >= M if any(a >= M for a in A): if N < K: print(A[N]) else: print(M) return # If N is within the initial terms if N < K: print(A[N]) return # Simulate the sequence until N or until a term >= M is found current = deque(A) for i in range(K, N + 1): new_term = max(min(current[m], B[m]) for m in range(K)) current.popleft() current.append(new_term) if new_term >= M: print(M) return print(current[-1]) if __name__ == "__main__": main()