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())) if N < K: print(A[N]) return # Initialize the deque with the first K elements dq = deque(A, maxlen=K) # We need to keep track of all elements until stabilization # or until N is reached. for i in range(K, N+1): # Compute the next element current_max = -float('inf') for j in range(K): a = dq[j] b = B[j] current_min = min(a, b) if current_min > current_max: current_max = current_min new_element = current_max # Add the new element to the deque dq.append(new_element) # Check if all elements in deque are the same as new_element all_same = True for elem in dq: if elem != new_element: all_same = False break if all_same: print(new_element) return # If we didn't break out early, output the last computed element print(dq[-1]) if __name__ == '__main__': main()