You are tasked with determining the eventual hardness of the last stone remaining in the annual Kode Festival contest. In this contest, 2^N stones with varying hardness levels participate in a knockout tournament. When two stones with hardness X and Y are thrown at each other, the stone with the lower hardness is destroyed, and the hardness of the surviving stone is adjusted accordingly. This process continues until only one stone remains. Your task is to find the eventual hardness of the last remaining stone. The input consists of N and the hardness values of the participating stones, and the output should be the eventual hardness of the last stone. The constraints are 1 ≤ N ≤ 18 and 1 ≤ A_i ≤ 10^9, where A_i is an integer.

# Buggy version:
N = int(input())
A = [int(input()) for _ in range(N)]

while len(A) > 1:
  B = []
  i = 0
  while i < len(A):
    if A[i] == A[i+1]:
      B.append(A[i])
    else:
      B.append(abs(A[i] - A[i+1]))      
    i += 2
    A = B

print(A[0])

# Fixed version:
N = int(input())
A = [int(input()) for _ in range(2**N)]

while len(A) > 1:
  B = []
  i = 0
  while i+1 < len(A):
    if A[i] == A[i+1]:
      B.append(A[i])
    else:
      B.append(abs(A[i] - A[i+1]))      
    i += 2
  A = B
print(A[0])

# Buggy version:
N = int(input())
A = []
for i in range(2**N):
    A.append(int(input()))
i = 2**N
while i > 1:
    i //= 2
    for j in range(i):
        l = 2*j
        r = 2*j+1
        print(A[l], A[r])
        if A[l] < A[r]:
            A[j] = A[r]-A[l]
        elif A[l] > A[r]:
            A[j] = A[l]-A[r]
        else:
            A[j] = A[r]
print(A[0])

# Fixed version:
N = int(input())
A = []
for i in range(2**N):
    A.append(int(input()))
i = 2**N
while i > 1:
    i //= 2
    for j in range(i):
        l = 2*j
        r = 2*j+1
        if A[l] < A[r]:
            A[j] = A[r]-A[l]
        elif A[l] > A[r]:
            A[j] = A[l]-A[r]
        else:
            A[j] = A[r]
print(A[0])

# Buggy version:
def setGroup(a):
    l = len(a)
    i = 0
    b = []
    while i < l:
        if a[i] < a[i+1]:
            b.append(a[i + 1] - a[i])
        elif  a[i] > a[i+1]:
            b.append(a[i] - a[i+1])
        else:
            b.append(a[i])
        i += 2
    return b
        
n = int(input())
i = 0
a =[]
while i < 2**n:
    a.append(int(input()))
    i += 1
while len(a) > 1:
    a = setGroup(a)
print(a)

# Fixed version:
def setGroup(a):
    l = len(a)
    i = 0
    b = []
    while i < l:
        if a[i] < a[i+1]:
            b.append(a[i + 1] - a[i])
        elif  a[i] > a[i+1]:
            b.append(a[i] - a[i+1])
        else:
            b.append(a[i])
        i += 2
    return b
        
n = int(input())
i = 0
a =[]
while i < 2**n:
    a.append(int(input()))
    i += 1
while len(a) > 1:
    a = setGroup(a)
print(a[0])

# Buggy version:
import sys

N = int(sys.stdin.readline().strip())

array = [None for _ in range(2**N)]
for i in range(int(2**N)):
    array[i] = int(sys.stdin.readline().strip())

for stage in range(N):
    for i in range(int(2**(N-stage-1))):
        tmp = abs(array[i*2] - array[i*2+1])
        array[i*2] = tmp if tmp != 0 else array[i*2]
        #print(array)

print(array[0])

# Fixed version:
import sys

N = int(sys.stdin.readline().strip())

array = [None for _ in range(2**N)]
for i in range(int(2**N)):
    array[i] = int(sys.stdin.readline().strip())

for stage in range(N):
    for i in range(int(2**(N-stage-1))):
        tmp = abs(array[i*2] - array[i*2+1])
        array[i] = tmp if tmp != 0 else array[i*2]
        #print(array)

print(array[0])