# a + sumA = b + sumB # a <= sumB, b <= sumA # b - a = sumA - sumB # max_{i \in N} (A_i + B_i - sumB) <= b def max(a,b); a > b ? a : b; end N = gets.to_i A = Array.new(N - 1,0) B = Array.new(N - 1,0) (N-1).times do |i| a, b = gets.split.map(&:to_i) A[i] = a B[i] = b end sumA = A.sum sumB= B.sum b_min = (N - 1).times.inject(0) do |min,i| max(min, A[i] + B[i] - sumB) end ans = (b_min .. sumA).count do |b| a = b - sumA + sumB a >= 0 && a <= sumB end puts ans