import numpy as np from scipy.sparse.csgraph import maximum_flow from scipy.sparse import csr_matrix from itertools import product def bisect(ng, ok, judge, eps=1): while abs(ng-ok) > eps: m = (ng+ok)//2 if judge(m): ok = m else: ng = m return ok def solve(ab): INF = 10**8 ab = [(INF,INF)]+ab n = len(ab) g = np.zeros((2*n+2,2*n+2), dtype=int) # 0: start, 1: goal, 2-(n+1): left, (n+2)-(2n-1): right for i,(a,b) in enumerate(ab): g[0,i+2] = a g[i+n+2,1] = b for i,j in product(range(n), repeat=2): if i == j: continue g[i+2,j+n+2] = INF res = maximum_flow(csr_matrix(g), 0, 1) max_t = res.flow_value ta = sum(a for a,b in ab[1:]) tb = sum(b for a,b in ab[1:]) def judge(t): g[0,2] = t-ta g[n+2,1] = t-tb return maximum_flow(csr_matrix(g), 0, 1).flow_value == t min_t = bisect(max(ta,tb)-1, max_t, judge) return max_t - min_t + 1 n = int(input()) ab = [tuple(map(int,input().split())) for _ in range(n-1)] print(solve(ab))