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 M = max(B) current = deque(A, maxlen=K) # Simulate up to 2*K steps beyond the initial K elements max_steps = 2 * K for step in range(K, max_steps + 1): if step > N: break # Check if current state can produce M has_condition = False for m in range(K): if B[m] == M and current[m] >= M: has_condition = True break if has_condition: print(M) return # Compute next value next_val = -float('inf') for m in range(K): val = min(current[m], B[m]) if val > next_val: next_val = val current.append(next_val) if step == N: print(next_val) return # After max_steps, check if all elements are <= B all_leq = True for m in range(K): if current[m] > B[m]: all_leq = False break if all_leq: X = max(current) print(X) else: # If not handled, return the last computed value (may not be correct for some cases) print(current[-1]) if __name__ == "__main__": main()