def main(): import sys input = sys.stdin.read().split() idx = 0 K = int(input[idx]); idx += 1 N = int(input[idx]); idx += 1 A = list(map(int, input[idx:idx+K])) idx += K B = list(map(int, input[idx:idx+K])) if N < K: print(A[N]) return current = A[:] # Current state: the last K elements seen = {} step = K - 1 # We are about to compute step K seen[tuple(current)] = step while True: step += 1 next_val = -float('inf') for j in range(K): prev_val = current[j] candidate = min(prev_val, B[j]) if candidate > next_val: next_val = candidate # Update current state: remove the first element, append next_val current = current[1:] + [next_val] current_tuple = tuple(current) if current_tuple in seen: # Cycle detected cycle_start = seen[current_tuple] cycle_length = step - cycle_start if N <= step: print(current[-1]) return remaining = N - cycle_start effective_remainder = remaining % cycle_length effective_step = cycle_start + effective_remainder # Simulate from cycle_start step forward by effective_remainder steps # We need to calculate the value at effective_step # Reset current to the state at cycle_start current = list(current_tuple) # The current in 'seen' is the one that leads to the cycle # Now simulate 'effective_remainder' steps for _ in range(effective_remainder): next_val = -float('inf') for j in range(K): prev_val = current[j] candidate = min(prev_val, B[j]) if candidate > next_val: next_val = candidate current = current[1:] + [next_val] print(current[-1]) return else: seen[current_tuple] = step # Check if we have reached N if step == N: print(current[-1]) return if __name__ == '__main__': main()