You still have partial information about the score during the historic football match. You are given a set of pairs $(a_i, b_i)$, indicating that at some point during the match the score was "$a_i$: $b_i$". It is known that if the current score is «$x$:$y$», then after the goal it will change to "$x+1$:$y$" or "$x$:$y+1$". What is the largest number of times a draw could appear on the scoreboard?

The pairs "$a_i$:$b_i$" are given in chronological order (time increases), but you are given score only for some moments of time. The last pair corresponds to the end of the match.


-----Input-----

The first line contains a single integer $n$ ($1 \le n \le 10000$) — the number of known moments in the match.

Each of the next $n$ lines contains integers $a_i$ and $b_i$ ($0 \le a_i, b_i \le 10^9$), denoting the score of the match at that moment (that is, the number of goals by the first team and the number of goals by the second team).

All moments are given in chronological order, that is, sequences $x_i$ and $y_j$ are non-decreasing. The last score denotes the final result of the match.


-----Output-----

Print the maximum number of moments of time, during which the score was a draw. The starting moment of the match (with a score 0:0) is also counted.


-----Examples-----
Input
3
2 0
3 1
3 4

Output
2

Input
3
0 0
0 0
0 0

Output
1

Input
1
5 4

Output
5



-----Note-----

In the example one of the possible score sequences leading to the maximum number of draws is as follows: 0:0, 1:0, 2:0, 2:1, 3:1, 3:2, 3:3, 3:4.

Difficulty: interview

Starter code:

Solutions:
def main():
    n = int(input())
    res = 0
    la, lb = 0, 0
    max_draw = -1
    for _ in range(n):
        a, b = [int(x) for x in input().split()]
        mx = max(la, lb)
        mn = min(a, b)
        if mx <= max_draw:
            mx = max_draw + 1
        if mx <= mn:
            res += mn - mx + 1
            max_draw = mn
        la, lb = a, b
    print(res)

def __starting_point():
    main()

__starting_point()

'''input
3
0 0
0 0
0 0

3
2 0
3 1
3 4


'''
n = int(input())
ans = 1
la = lb = 0
for i in range(n):
	a, b = list(map(int, input().split()))
	if max(la, lb) <= min(a, b):
		ans += min(a, b) - max(la, lb) + 1 - (la == lb) 
	la, lb = a, b

print(ans)

ii = lambda: int(input())
mi = lambda: list(map(int, input().split()))
li = lambda: list(mi())

n = ii()
x, y = -1, -1
ans = 0
for _ in range(n):
    a, b = mi()
    if x > y:
        if b >= x:
            ans += 1
            y = x
    elif y > x:
        if a >= y:
            ans += 1
            x = y
    if x == y:
        ans += min(a, b) - x
    x, y = a, b
print(ans)


s1 = s2 = 0
draws = 1
ld = 0
for _ in range(int(input())):
	n1, n2 = map(int, input().split())
	x = min(n1, n2)-max(s1, s2)
	if x >= 0:
		if max(s1, s2) > ld:
			draws+=1
		draws += x
		ld = min(n1, n2)
	s1, s2 = n1, n2
print(draws)

n = int(input())
x = y = k = 0
ans = 1
for i in range(n):
    a, b = map(int, input().split())
    if (x, y) == (a, b): continue
    ans += max(0, min(a, b) - max(x - 1, y - 1, k))
    k = min(a, b)
    x, y = a, b
print(ans)

# -*- coding: utf-8 -*-
# @Time    : 2019/2/23 18:26
# @Author  : LunaFire
# @Email   : gilgemesh2012@gmail.com
# @File    : B. Draw!.py


def main():
    n = int(input())
    pre_a, pre_b, ret = 0, 0, 1
    for _ in range(n):
        curr_a, curr_b = map(int, input().split())
        if pre_a == pre_b:
            ret += min(curr_a, curr_b) - pre_a
        else:
            ret += max(0, min(curr_a, curr_b) - max(pre_a, pre_b) + 1)
        pre_a, pre_b = curr_a, curr_b
    print(ret)


def __starting_point():
    main()
__starting_point()

import sys
readline = sys.stdin.readline

ans = 0
N = int(readline())
pa = pb = 0
cur = -1
for i in range(N):
    a, b = map(int, readline().split())
    ans += max(0, min(a, b) - max(pa, pb) + 1)
    mi = min(a, b) + 1
    pa, pb = max(mi, a), max(mi, b)
print(ans)

N = int(input())
src = [tuple(map(int,input().split())) for i in range(N)]
ans = 1
pa = pb = 0
for a,b in src:
    if pa == pb:
        ans += min(a,b) - pa
    elif pa < pb:
        if min(a,b) >= pb:
            ans += min(a,b) - pb + 1
    else:
        if min(a,b) >= pa:
            ans += min(a,b) - pa + 1
    pa,pb = a,b
print(ans)


from math import *
import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

n = mint()
pa, pb = 0,0
pl = 0
c = 0
for i in range(n):
	a, b = mints()
	l = max(max(pa,pb),pl)
	r = min(a,b)
	#print(l,r)
	if r >= l:
		c += r-l+1
		pl = r+1
	pa, pb = a, b
print(c)

import sys
N = int(input())
count = 1
score_a, score_b = 0, 0

for a, b in (map(int, l.split()) for l in sys.stdin):
    if score_a > score_b:
        score_b = min(score_a, b)
        if score_a == score_b:
            count += 1
    elif score_a < score_b:
        score_a = min(score_b, a)
        if score_a == score_b:
            count += 1

    if score_a < min(a, b):
        count += min(a, b) - score_a

    score_a, score_b = a, b

print(count)

n = int(input())
a0, b0 = list(map(int,input().split()))
wynik = min(a0,b0) + 1
for i in range(n-1):
	a1, b1 = list(map(int,input().split()))
	wynik += max(0, max(0, 1 + min(a1,b1) - max(a0,b0) ))
	if a0 == b0:
		wynik -= 1
	a0 = a1
	b0 = b1
print(wynik)

n = int(input())
res = 1
t1 = 0
t2 = 0
for i in range(n):
    new_t1, new_t2 = list(map(int, input().split()))
    if t1 == t2:
        res += min(new_t1 - t1, new_t2 - t2)
    elif t1 > t2:
        if new_t1 > new_t2:
            res += max(0, new_t2 - t1 + 1)
        else:
            res += new_t1 - t1 + 1
    elif t2 > t1:
        if new_t2 > new_t1:
            res += max(0, new_t1 - t2 + 1)
        else:
            res += new_t2 - t2 + 1
    t1, t2 = new_t1, new_t2
print(res)



n = int(input())

arr = []

for i in range(n):
    a,b = list(map(int,input().strip().split()))
    arr.append([a,b])

ans = 1
la = [0,0]

for i in range(n):
    #print(ans)
    if i==0:
        ans+=min(arr[i][0],arr[i][1])
    else:
        if arr[i-1][0]==arr[i-1][1]:
            c = 0
        else:
            c = 1
        ans+=max(0,c+min(arr[i][0],arr[i][1])-max(arr[i-1][0],arr[i-1][1]))
print(ans)


n = int(input())
a0, b0 = 0, 0
ans = 1
for i in range(n):
    a, b = map(int, input().split())
    ans += max(min(a, b) - max(a0, b0), 0)
    if min(a, b) - max(a0, b0) >= 0 and a0 != b0:
        ans += 1
    a0, b0 = a, b
print(ans)

n=int(input())
c=d=counter=0
for i in range(n):
    a,b=[int(x) for x in input().split()]
    if c!=d or i==0:
        counter+=max(min(a,b)-max(c,d)+1,0)
    else:
        counter+=max(min(a,b)-max(c,d),0)
    c,d=a,b
print(counter)


n = int(input())

a, b = map(int, input().split())
ans = min(a, b) + 1
for i in range(n - 1):
    c, d = map(int, input().split())
    if c > a or d > b:
        e = max(a, b)
        f = min(c, d)
        ans += max(0, f - e + 1)
        if a == b:
            ans -= 1
        #print(ans)
    a, b = c, d
    
print(ans)

n = int(input())
num = 1
prex, prey = 0, 0
for i in range(n):
    x, y = map(int, input().split())
    if prex == prey:
        num += min(x - prex, y - prey)
        prex, prey = x, y
    elif prex < prey:
        if x >= prey:
            prex = prey
            num += 1
            num += min(x - prex, y - prey)
            prex, prey = x, y
        else:
            prex, prey = x, y
    else:
        if y >= prex:
            prey = prex
            num += 1
            num += min(x - prex, y - prey)
            prex, prey = x, y
        else:
            prex, prey = x, y

print(num)

from sys import stdin, stdout
from math import *
from heapq import *
from collections import *

def main():
    n=int(stdin.readline())
    a=[0]*(n+1)
    b=[0]*(n+1)
    res=1
    for i in range(1,n+1):
        a[i],b[i]=[int(x) for x in stdin.readline().split()]
        if (a[i-1]==b[i-1]):
            res+= (min(a[i],b[i])-a[i-1])
        else:
            res+=max(0,min(a[i],b[i])-max(a[i-1],b[i-1])+1)
    stdout.write(str(res))
    return 0

def __starting_point():
    main()
__starting_point()

def change(score1,score2):
	if score1==None:
		return min(score2)+1
	max_1=max(score1[0],score2[0])
	max_2=max(score1[1],score2[1])
	min_1=min(score1[0],score2[0])
	min_2=min(score1[1],score2[1])
	return max(0,min(max_1,max_2)-max(min_1,min_2)+(not(score1[0]==score1[1])))
scores=None
ans=0
for i in range(int(input())):
	temp=[int(i) for i in input().split()]
	if temp==scores:
		continue
	ans+=change(scores,temp)
	scores=temp[:]
print(ans)


import math as mt
import itertools as it
import functools as ft
import random as rnd

stdin = lambda type_ = "int", sep = " ": list(map(eval(type_), input().split(sep)))
joint = lambda sep = " ", *args: sep.join(str(i) if type(i) != list else sep.join(map(str, i)) for i in args)

n = int(input())

a, b = 0, 0

hap = set()
hap.add(0)
cnt = 1

for i in range(n):
    aa, bb = stdin()
    if max(a, b) <= min(aa, bb):
        cnt += max(-max(a, b) + min(aa, bb) - 1, 0)
        if max(a, b) not in hap:
            cnt += 1
            hap.add(max(a, b))
        if min(aa, bb) not in hap:
            cnt += 1
            hap.add(min(aa, bb))
    a, b = aa, bb

print(cnt)

n = int(input())
last = 0
total = 1
for _ in range(n):
    a,b = list(map(int,input().split()))
    min_score = min(a,b)
    max_score = max(a,b)
    if min_score > last:
        total += (min_score-last)  
    if a == b:
        last = a
    else:
        last = max(0,max_score-1)
print(total)


n = int(input())
a_0 = b_0 = 0
r = 1
for _ in range(n):
    a, b = list(map(int, input().split()))

    if a_0 > b_0:
        if a > b:
            r += max(0, b - a_0 + 1)
        else:
            r += a - a_0 + 1
    elif a_0 < b_0:
        if b > a:
            r += max(0, a - b_0 + 1)
        else:
            r += b - b_0 + 1
    else:
        r += min(a, b) - a_0
    a_0, b_0 = a, b
print(r)


n = int(input())
a = []
b = []
for i in range(n):
    A, B = list(map(int, input().split()))
    a.append(A)
    b.append(B)

count = 1
sa = 0
sb = 0
for i in range(n):
    if sa != sb:
        if min(a[i], b[i]) >= max(sa, sb):
            ma = max(sa, sb)
            sa = ma
            sb = ma
            count += 1
        else:
            sa = a[i]
            sb = b[i]
            continue
    count += min(a[i], b[i]) - sa
    sa = a[i]
    sb = b[i]

print(count)



import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))

N = int(input())

ans = 1
a, b = 0, 0

for _ in range(N):
    c, d = inpl()
    candi = []
    if a == b:
        ans -= 1
    if a <= d <= c:
        candi.append(d)
    if a <= b <= c:
        candi.append(b)
    if b <= a <= d:
        candi.append(a)
    if b <= c <= d:
        candi.append(c)
    if len(candi) >= 2:
        maxi = max(candi)
        mini = min(candi)
        ans += maxi - mini + 1
    a, b = c, d
print(ans)