def main(): import sys 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) if N < K: print(A[N]) return if any(a >= M for a in A): print(M) return current = A.copy() max_steps = K * K + K # Arbitrary large enough steps to detect stabilization for i in range(K, max_steps + 1): next_val = max(min(current[j], B[j]) for j in range(K)) if next_val == M: print(M) return current.pop(0) current.append(next_val) if all(x == current[0] for x in current): x = current[0] print(x if x <= M else M) return if i == N: print(next_val) return # If not stabilized, which shouldn't happen for the problem's constraints print(current[-1]) if __name__ == "__main__": main()