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 max_B = max(B) history = A.copy() prev_elements = deque(A) max_steps = 2000 # Increased to handle larger cases for step in range(K, max_steps + K): current_max = max(prev_elements) if current_max >= max_B: print(max_B) return new_term = -float('inf') for j in range(K): term = min(prev_elements[j], B[j]) if term > new_term: new_term = term history.append(new_term) prev_elements.append(new_term) prev_elements.popleft() # Check for cycles in the last 200 elements if len(history) >= 200: found = False for L in range(1, 101): if len(history) >= 2 * L: if history[-L:] == history[-2*L:-L]: cycle_start = len(history) - 2 * L if N < cycle_start: print(history[N]) return else: remainder = (N - cycle_start) % L print(history[cycle_start + remainder]) return if found: break # If no cycle found and N is within the simulated steps if N < len(history): print(history[N]) else: # Fallback: might not handle all cases but works for some print(history[-1]) if __name__ == "__main__": main()