import sys input = sys.stdin.buffer.readline from heapq import heappop, heappush n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) bigpq = [] smallpq = [] # initialize # a,b,half(a,b)の順に3*i, 3*i+1, 3*i+2のidxをふることにする for i in range(n): a, b = A[i], B[i] if a > b: heappush(bigpq, (-b, 3 * i + 1)) heappush(smallpq, (b, 3 * i + 1)) else: heappush(bigpq, (-a, 3 * i)) heappush(smallpq, (a, 3 * i)) cnt = [0] * n ans = 1 << 100 finished = set() while True: # print(ans) # print(smallpq) # print(bigpq) now_minimum, smallidx = heappop(smallpq) # smallidxのものはbiggestとして使えない finished.add(smallidx) # 使用済みbigpqだとアウト while bigpq: if bigpq[0][1] in finished: heappop(bigpq) else: break # 使える最大候補がない if not bigpq: break biggest, bigidx = bigpq[0] biggest *= -1 ans = min(ans, biggest - now_minimum) biggest *= -1 # smallidxと同じiのもので、次のものを追加する。 idx = smallidx // 3 cnt[idx] += 1 a, b = A[idx], B[idx] if cnt[idx] == 1: v = (a + b) // 2 heappush(bigpq, (-v, 3 * idx + 2)) heappush(smallpq, (v, 3 * idx + 2)) elif cnt[idx] == 2: if a > b: heappush(bigpq, (-a, 3 * idx)) heappush(smallpq, (a, 3 * idx)) else: heappush(bigpq, (-b, 3 * idx + 1)) heappush(smallpq, (b, 3 * idx + 1)) else: break print(ans)