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.

Source: codeforces | Difficulty: MEDIUM
Tags: Implementation

Greedy algorithms


Solutions:
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())
pa = pb = 0
ans = 1
s = {0}
for i in range(n):
	(a, b) = map(int, input().split())
	u = min(a, b)
	d = max(pa, pb)
	ans += max(u - d, 0)
	if u - d >= 0:
		if d not in s:
			ans += 1
			s.add(d)
		s.add(u)
	(pa, pb) = (a, b)
print(ans)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(beauty, s, n, count):
	pass

def sortSecond(val):
	return val[1]
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect

def solve():
	n = scanner()
	c = 0
	goals = []
	for i in range(n):
		(a, b) = read()
		goals.append([a, b])
	mp = {}
	lmp = {}
	for i in range(1, n):
		last = goals[i - 1]
		now = goals[i]
		val = str(now[0]) + str(now[1])
		if mp.get(val) == True:
			continue
		if str(last[0]) + str(last[1]) == '00':
			lmp[0] = True
		mp[val] = True
		mlast = max(last[0], last[1])
		mnow = min(now[0], now[1])
		c += max(0, mnow - mlast + 1)
		if last[0] == last[1]:
			c -= 1
	if lmp.get(0) == None:
		c += min(goals[0][0], goals[0][1]) + 1
	elif lmp.get(0) == True:
		c += 1
	print(c)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(1):
	solve()


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()


n = int(input())
draw = 0
maxSkor = 0
minSkorLast = 0
maxSkorLast = 0
for moment in range(n):
	skor = [int(item) for item in input().split(' ')]
	minSkor = min(skor[0], skor[1])
	maxSkor = max(skor[0], skor[1])
	drawPoin = minSkor - maxSkorLast + 1
	if drawPoin > 0:
		if maxSkorLast != minSkorLast:
			draw = draw + drawPoin
		else:
			draw = draw + drawPoin - 1
	minSkorLast = minSkor
	maxSkorLast = maxSkor
print(draw + 1)


cnt = 0
arr = []
brr = []
arr.append(0)
brr.append(0)
x = 0
y = 0
l = 0
n = int(input())
for i in range(n):
	(a, b) = list(map(int, input().split()))
	if a == x and b == y:
		continue
	arr.append(a)
	brr.append(b)
	l += 1
	x = a
	y = b
for i in range(l):
	a = max(arr[i], brr[i])
	b = min(arr[i + 1], brr[i + 1])
	if a > b:
		continue
	cnt += b - a + 1
	if i > 0 and arr[i] == brr[i]:
		cnt -= 1
print(max(1, cnt))


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)


n = int(input())
r = 1
a = b = 0
for _ in range(n):
	(ai, bi) = map(int, input().split())
	if a == ai and b == bi:
		continue
	if a > bi or b > ai:
		b = bi
		a = ai
		continue
	if a < b <= ai:
		r += 1
		a = b
	if b < a <= bi:
		r += 1
		b = a
	if a == b:
		if ai > bi:
			r += bi - b
		else:
			r += ai - a
	a = ai
	b = bi
print(r)


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
P = lambda x: stdout.write(x)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
N = 10 ** 9 + 7
l = pst = 0
ans = 1
for _ in range(I()):
	u = list(R())
	u1 = max(u)
	u = min(u)
	if u >= l:
		ans += u - l
		if l != pst:
			ans += 1
		pst = u
	l = u1
print(ans)


n = int(input())
count = 1
prev = (0, 0)
for i in range(n):
	now = tuple(map(int, input().split()))
	d = min(now) - max(prev)
	if d >= 0:
		count += d + int(prev[0] != prev[1])
	prev = now
print(count)


def fn(s1, s2):
	(x1, y1) = s1
	(x2, y2) = s2
	if x1 == y1:
		x1 += 1
		y1 += 1
	k = min(x2, y2) - max(x1, y1)
	tot = 0
	if k >= 0:
		tot = k + 1
	return tot
n = int(input())
inp = [(-1, -1)]
for _ in range(n):
	(x, y) = [int(i) for i in input().split()]
	inp.append((x, y))
tot = 0
for (s1, s2) in zip(inp, inp[1:]):
	tot += fn(s1, s2)
print(tot)


n = int(input())
res = 1
(c, d) = (0, 0)
for i in range(n):
	(a, b) = map(int, input().split())
	if a != c or b != d:
		bf = min(a, b) - max(c, d)
		if min(a, b) - max(c, d) >= 0 and c != d:
			bf += 1
		res += max(bf, 0)
	(c, d) = (a, b)
print(res)


n = int(input())
l = [[0, 0]]
for _ in range(n):
	l += [list(map(int, input().split()))]
r = 1
for i in range(n):
	k = max(l[i][0], l[i][1])
	t = min(l[i + 1][0], l[i + 1][1])
	k = k + 1 if l[i][0] == l[i][1] else k
	r = r + t - k + 1 if t >= k else r
print(r)


n = int(input())
l = []
for _ in range(n):
	l += [list(map(int, input().split()))]
a = 0
b = 0
draws = 1
for score in l:
	while [a, b] != score:
		if a < b and a < score[0]:
			a = min(b, score[0])
			if a == b:
				draws += 1
				continue
		if b < a and b < score[1]:
			b = min(a, score[1])
			if a == b:
				draws += 1
				continue
		if a == score[0] and b < score[1]:
			if b < a <= score[1]:
				draws += 1
			b = score[1]
			continue
		if b == score[1] and a < score[0]:
			if a < b <= score[0]:
				draws += 1
			a = score[0]
			continue
		if a == b:
			draws += min(score) - a
			a = score[0]
			b = score[1]
print(draws)


n = int(input())
score = (0, 0)
count = 1
for i in range(n):
	newscore = tuple(map(int, input().split()))
	(a, b, x, y) = (newscore[0], newscore[1], score[0], score[1])
	count += max(min(a, b) - max(x, y) + (1 if x != y else 0), 0)
	score = newscore[:]
print(count)


n = int(input())
(x, y) = map(int, input().split())
if x == 0 or y == 0:
	ans = 1
else:
	ans = min(x, y) + 1
for i in range(n - 1):
	(a, b) = map(int, input().split())
	if x > y:
		if b > x:
			ans += min(b, a) - x + 1
		if b == x and b != y:
			ans += 1
	elif y > x:
		if a > y:
			ans += min(b, a) - y + 1
		if a == y and a != x:
			ans += 1
	else:
		ans += min(a, b) - x
	(x, y) = (a, b)
print(ans)


def solver(a, b, x, y):
	if min(x, y) >= max(a, b):
		return min(x, y) - max(a, b) + 1
	return 0
n = int(input())
(a, b) = (0, 0)
res = 0
for i in range(n):
	(x, y) = [int(xx) for xx in input().split()]
	if x == y and i < n - 1:
		res -= 1
	res += solver(a, b, x, y)
	(a, b) = (x, y)
print(res)


from sys import stdin, stdout
from math import sqrt, pi, acos, gcd, atan2, cos, sin

def sqr(a):
	return a * a

def dist(a, b):
	return sqrt(sqr(a[0] - b[0]) + sqr(a[1] - b[1]))

def s_dist(a, b):
	return sqr(a[0] - b[0]) + sqr(a[1] - b[1])

def trans(p, al):
	return (p[0] * cos(al) + p[1] * sin(al), -p[0] * sin(al) + p[1] * cos(al))
ans = 0
n = int(stdin.readline())
prev = (0, 0)
for i in range(n):
	(a, b) = map(int, stdin.readline().split())
	if i + 1 != n and a == b:
		ans -= 1
	if a <= b and prev[0] <= prev[1]:
		ans += max(0, a - prev[1] + 1)
	elif a >= b and prev[0] >= prev[1]:
		ans += max(0, b - prev[0] + 1)
	elif a >= b and prev[0] <= prev[1]:
		ans += b - prev[1] + 1
	else:
		ans += a - prev[0] + 1
	prev = (a, b)
stdout.write(str(ans))


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)


n = int(input())
a = 0
b = 0
ma = 0
ans = 1
for i in range(n):
	(c, d) = map(int, input().split())
	ma = max(a, b)
	mi = min(c, d)
	ans += max(0, mi - ma + int(a != b))
	(a, b) = (c, d)
print(ans)


n = int(input())
a = [[0, 0]] + [[int(t) for t in input().split(' ')] for _ in range(n)]
was = set()
total = 0
for i in range(n):
	pair = (max(a[i]),) * 2
	if pair[0] > a[i + 1][0] or pair[1] > a[i + 1][1]:
		continue
	if pair not in was:
		was.add(pair)
		total += 1
	total += max(min(a[i + 1]) - max(a[i]), 0)
	was.add((min(a[i + 1]),) * 2)
	last = a[i]
print(total)


(x, y) = (0, 0)
ans = 1
for _ in range(int(input())):
	(xi, yi) = map(int, input().split())
	f = 0
	if x != y:
		f = 1
	ans += max(0, min(xi, yi) - max(x, y) + f)
	(x, y) = (xi, yi)
print(ans)


import os
import sys
from io import BytesIO, IOBase
import math
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
(a, b) = map(int, input().split())
ans = 1
ans += min(a, b)
for i in range(1, n):
	(c, d) = map(int, input().split())
	f = 0
	if a != b:
		f = 1
	ans += max(0, min(c, d) - max(a, b) + f)
	a = c
	b = d
print(ans)


n = int(input())
a1 = 0
b1 = 0
total = 1
for i in range(n):
	tokens = input().split()
	a2 = int(tokens[0])
	b2 = int(tokens[1])
	if b1 > a1:
		count = a2 - b1
		if count < 0:
			a1 = a2
			b1 = b2
			continue
		else:
			total = total + min(1 + count, b2 - b1 + 1)
			a1 = a2
			b1 = b2
			continue
	elif a1 > b1:
		count = b2 - a1
		if count < 0:
			a1 = a2
			b1 = b2
			continue
		else:
			total = total + min(1 + count, a2 - a1 + 1)
			a1 = a2
			b1 = b2
			continue
	else:
		total = total + min(a2 - a1, b2 - b1)
		a1 = a2
		b1 = b2
		continue
print(total)


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


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)


def solve(scores):
	last_draw = 0
	draws = 1
	(prev_a, prev_b) = (0, 0)
	for (a, b) in scores:
		intersect_beg = max(prev_a, prev_b)
		intersect_beg = max(intersect_beg, last_draw + 1)
		intersect_end = min(a, b)
		if intersect_end >= intersect_beg:
			draws += intersect_end - intersect_beg + 1
			last_draw = intersect_end
		(prev_a, prev_b) = (a, b)
	return draws
n = int(input())
scores = [None] * n
for i in range(n):
	scores[i] = tuple(map(int, input().split()))[:2]
print(solve(scores))


n = int(input())
a_0 = b_0 = 0
r = 1
for _ in range(n):
	(a, b) = 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)


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 noones(lst):
	draws = 1
	(x, y) = (0, 0)
	for elem in lst:
		draws += max(min(elem[0], elem[1]) - max(x, y) + 1, 0)
		if x == y:
			draws -= 1
		(x, y) = (elem[0], elem[1])
	return draws
n = int(input())
z = list()
for i in range(n):
	(a, b) = [int(j) for j in input().split()]
	z.append([a, b])
print(noones(z))


n = int(input())
arr = []
arr.append((0, 0))
ans = 1
for _ in range(n):
	(a, b) = map(int, input().split())
	arr.append((a, b))
for i in range(1, n + 1):
	preva = arr[i - 1][0]
	prevb = arr[i - 1][1]
	cura = arr[i][0]
	curb = arr[i][1]
	if preva == cura and prevb == curb:
		pass
	elif preva == prevb:
		ans += min(cura, curb) - preva
	else:
		ans += max(0, min(cura, curb) - max(preva, prevb) + 1)
print(ans)


from __future__ import division, print_function
from sys import stdin, stdout
import itertools, os, sys, threading
from collections import deque, Counter, OrderedDict, defaultdict
import heapq
from math import ceil, floor, log, sqrt, factorial, pow, pi, gcd
from fractions import Fraction

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def fii():
	return int(stdin.readline())

def fsi():
	return str(stdin.readline())

def fmi():
	return map(int, stdin.readline().split())

def fli():
	return list(fmi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def dfs(v, c):
	if visited[v]:
		if col[v] != c:
			print('-1')
			exit()
		return
	col[v] = c
	visited[v] = 1
	for i in graph[v]:
		dfs(i, c ^ 1)

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d.keys():
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = {}
	for i in range(n):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans

def primeFactors(n):
	s = set()
	while n % 2 == 0:
		s.add(2)
		n = n // 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			s.add(i)
			n = n // i
	if n > 2:
		s.add(n)
	return s

def find_all(a_str, sub):
	start = 0
	while True:
		start = a_str.find(sub, start)
		if start == -1:
			return
		yield start
		start += len(sub)
n = ii()
count = 1
(px, py, f) = (0, 0, 0)
for i in range(n):
	(x, y) = mi()
	if px == py:
		f = 0
	else:
		f = 1
	count += max(min(x, y) - max(px, py) + f, 0)
	(px, py) = (x, y)
print(count)


n = int(input())
cnt = 0
(ca, cb) = (-1, -1)
for i in range(n):
	(a, b) = map(int, input().split())
	v = max(min(a, b) - max(ca, cb) + (1 if ca != cb else 0), 0)
	cnt += v
	ca = a
	cb = b
print(cnt)


N = int(input())
out = 1
a = 0
b = 0
for c in range(N):
	[ai, bi] = [int(c) for c in input().split()]
	if a != ai and b != bi or c == 0:
		out += max(0, min(ai, bi) - max(a, b) + 1 - (a == b))
	elif a != ai or b != bi:
		out += a > b and bi >= ai or (b > a and ai >= bi)
	a = ai
	b = bi
print(out)


import sys
N = int(input())
a1 = 0
b1 = 0
count = 1
line = [[int(c) for c in l.split()] for l in sys.stdin]
for i in range(N):
	a = line[i][0]
	b = line[i][1]
	if a == a1 and b == b1:
		pass
	elif a1 == b1:
		if a >= b:
			count += b - b1
		else:
			count += a - a1
	elif a1 > b1:
		if b >= a1:
			if b >= a:
				count += a - a1 + 1
			else:
				count += b - a1 + 1
		else:
			pass
	elif b1 > a1:
		if a >= b1:
			if a >= b:
				count += b - b1 + 1
			else:
				count += a - b1 + 1
		else:
			pass
	a1 = a
	b1 = b
print(count)


from collections import deque, Counter, OrderedDict
from heapq import nsmallest, nlargest
from math import ceil, floor, log, log2, sqrt, gcd, factorial, pow, pi

def binNumber(n, size=4):
	return bin(n)[2:].zfill(size)

def iar():
	return list(map(int, input().split()))

def ini():
	return int(input())

def isp():
	return map(int, input().split())

def sti():
	return str(input())

def par(a):
	print(' '.join(list(map(str, a))))

def tdl(outerListSize, innerListSize, defaultValue=0):
	return [[defaultValue] * innerListSize for i in range(outerListSize)]

class pair:

	def __init__(self, f, s):
		self.fi = f
		self.se = s

	def __lt__(self, other):
		return (self.fi, self.se) < (other.fi, other.se)
n = ini()
p = []
p.append([0, 0])
s = 1
for _ in range(n):
	(x, y) = isp()
	p.append([x, y])
for i in range(1, n + 1):
	if p[i] != p[i - 1]:
		s += max(0, min(p[i]) - max(p[i - 1]) + 1)
		if p[i - 1] == [p[i - 1][0]] * 2:
			s -= 1
print(s)


n = int(input())
draws = 1
(a1, b1) = [int(x) for x in input().split()]
draws += min(a1, b1)
prev_was_draw = a1 == b1
for _ in range(n - 1):
	(a2, b2) = [int(x) for x in input().split()]
	if a1 != a2 or b1 != b2:
		d = min(a2, b2) - max(a1, b1)
		d += 1 if not prev_was_draw else 0
		prev_was_draw = a2 == b2
		draws += d if d > 0 else 0
	(a1, b1) = (a2, b2)
print(draws)


def main():
	n = int(input())
	(a1, b1) = (0, 0)
	ans = 1
	for i in range(n):
		(a2, b2) = map(int, input().split())
		if a2 == a1 and b2 == b1:
			continue
		ans += max(0, min(a2, b2) - max(a1, b1) + 1)
		if a1 == b1:
			ans -= 1
		a1 = a2
		b1 = b2
	print(ans)
main()


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)


from collections import defaultdict
t = int(input())
start = 0
prev = 0
ans = 1
lis = []
Dict = defaultdict(int)
for i in range(t):
	(a, b) = [int(i) for i in input().split()]
	if i == 0:
		mi = min(a, b)
		ma = max(a, b)
		ans += max(0, mi - prev)
		prev = max(mi, ma - 1)
		Dict[a, b] = 1
	elif Dict[a, b] == 0:
		mi = min(a, b)
		ma = max(a, b)
		ans += max(0, mi - prev)
		prev = max(mi, ma - 1)
		Dict[a, b] = 1
print(ans)


count = 1
(a, b) = (0, 0)
for i in range(int(input())):
	(x, y) = map(int, input().split())
	count += max(0, min(x, y) - max(a, b) + (a != b))
	(a, b) = (x, y)
print(count)


from sys import stdin
input = stdin.buffer.readline
n = int(input())
a = [[0, 0]] + [[*map(int, input().split())] for _ in range(n)]
ans = 1
for i in range(1, len(a)):
	if a[i] == a[i - 1]:
		continue
	ans += max(0, min(a[i]) - max(a[i - 1]) + (a[i - 1][0] != a[i - 1][1]))
print(max(1, ans))


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


n = int(input())
p = 0
large = 0
c = 0
d = 0
for i in range(n):
	(a, b) = map(int, input().split())
	small = min(a, b)
	if small >= large and c == d and (d != 0) and (c != a or d != b) and (a != 0 or b != 0):
		p += small - large
	elif small >= large and (c != a or d != b) and (a != 0 or b != 0):
		p += small - large + 1
	large = max(a, b)
	c = a
	d = b
if p == 0:
	print('1')
else:
	print(p)


n = int(input())
(a, b) = (0, 0)
ans = 1
for i in range(n):
	(new_a, new_b) = map(int, input().split())
	if min(new_a, new_b) - max(a, b) < 0:
		pass
	elif a == b:
		ans += max(0, min(new_a, new_b) - max(a, b))
	else:
		ans += max(0, min(new_a, new_b) - max(a, b)) + 1
	(a, b) = (new_a, new_b)
print(ans)


(a, b) = (0, 0)
c = 1
n = int(input())
for _ in range(n):
	(x, y) = map(int, input().split())
	c += max(0, min(x, y) - max(a, b) + (a != b))
	(a, b) = (x, y)
print(c)


ii = lambda : int(input())
mi = lambda : map(int, input().split())
(ai, bi) = (0, 0)
d = 1
for _ in range(ii()):
	(an, bn) = mi()
	d += max(0, min(an, bn) - max(ai, bi) + (ai != bi))
	(ai, bi) = (an, bn)
print(d)


n = int(input())
c = 's'
d = 1
(pa, pb) = (0, 0)
for i in range(n):
	(a, b) = map(int, input().split())
	if pa == a and pb == b:
		continue
	if a < pb or b < pa:
		(pa, pb) = (a, b)
		continue
	d += a - pa + 1 + b - pb + 1 - (max(a, b) - min(pa, pb) + 1)
	if pa == pb:
		d -= 1
	(pa, pb) = (a, b)
print(d)


count = int(input())
lst = [[0, 0]]
for i in range(count):
	lst.append(input().split())
	lst[i + 1][0] = int(lst[i + 1][0])
	lst[i + 1][1] = int(lst[i + 1][1])
sum = 0
for i in range(count):
	sum += max(min(lst[i + 1][0] - lst[i][0], lst[i + 1][0] - lst[i][1], lst[i + 1][1] - lst[i][0], lst[i + 1][1] - lst[i][1]) + 1, 0)
	if lst[i][0] == lst[i][1] and i > 0:
		sum -= 1
if lst[count][0] == 0 and lst[count][1] == 0:
	sum = 1
print(sum)


import sys

def draw(a0, b0, a1, b1):
	d = 0
	if a0 < b0:
		if a1 > b0:
			d = min(b1 - b0, a1 - b0) + 1
		elif a1 == b0:
			d = 1
	elif b0 < a0:
		if b1 > a0:
			d = min(a1 - a0, b1 - a0) + 1
		elif b1 == a0:
			d = 1
	elif a0 == b0:
		d = min(a1 - a0, b1 - b0)
	return d
first = True
s = 1
a0 = 0
b0 = 0
for line in sys.stdin:
	if first:
		first = False
		continue
	l = line.strip().split(' ')
	l = [int(x) for x in l]
	a1 = l[0]
	b1 = l[1]
	s += draw(a0, b0, a1, b1)
	a0 = a1
	b0 = b1
print(s)


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)


def intersect(t1, t2, y1, y2):
	if t1 == y1 and t2 == y2:
		return 0
	if t1 == y1:
		if t2 >= t1 or (t2 < t1 and y2 < t1):
			return 0
		if t2 < t1 and y2 >= t1:
			return 1
	if t2 == y2:
		if t1 >= t2 or (t1 < t2 and y1 < t2):
			return 0
		if t1 < t2 and y1 >= t2:
			return 1
	if t1 == t2:
		return min(y1, y2) - t1
	if y1 < t2:
		return 0
	if t1 > y2:
		return 0
	if y1 == t2:
		return 1
	if t1 == y2:
		return 1
	count = min(y1, y2) - max(t1, t2) + 1
	return count
x = int(input())
count = 1
(t1, t2) = (0, 0)
for i in range(x):
	y = input().split()
	(y1, y2) = (int(y[0]), int(y[1]))
	count += intersect(t1, t2, y1, y2)
	(t1, t2) = (y1, y2)
print(count)


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


def f(a, b, c, d):
	return max(0, min(c, d) - max(a, b) + 1)
n = int(input(''))
a = [list(map(int, input('').split())) for i in range(n)]
k = f(0, 0, a[0][0], a[0][1])
for i in range(len(a) - 1):
	k += f(a[i][0], a[i][1], a[i + 1][0], a[i + 1][1])
for i in range(len(a) - 1):
	if a[i][0] == a[i][1]:
		k -= 1
print(k)


try:
	s = int(input())
	(u, v) = map(int, input().split())
	total = min(u, v) + 1
	for i in range(s - 1):
		(x, y) = map(int, input().split())
		if x == u and y == v:
			continue
		elif u == v:
			total += max(min(x, y) - max(u, v), 0)
		else:
			total += max(min(x, y) - max(u, v) + 1, 0)
		(u, v) = (x, y)
	print(total)
except e:
	pass


xi = 0
yi = 0
ans = 1
for _ in range(int(input())):
	(xf, yf) = map(int, input().split())
	m1 = max(xi, yi)
	m2 = min(xf, yf)
	if xi == yi:
		ans = ans + min(xf - xi, yf - xi)
	else:
		ans = ans + max(m2 - m1 + 1, 0)
	xi = xf
	yi = yf
print(ans)


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
	(a, b) = (c, d)
print(ans)


n = int(input())
l = [[0, 0]]
count = 0
for i in range(n):
	(a, b) = map(int, input().split())
	l.append([a, b])
for i in range(n):
	(a, b) = l[i]
	(c, d) = l[i + 1]
	if a >= b:
		k1 = a - b
		k2 = d - b
		k3 = min(c, d)
		if k2 >= k1:
			if c != d:
				count += k3 + 1 - a
			else:
				count += k3 + 1 - a - 1
	else:
		(b, a) = (a, b)
		(d, c) = (c, d)
		k1 = a - b
		k2 = d - b
		k3 = min(c, d)
		if k2 >= k1:
			if c != d:
				count += k3 + 1 - a
			else:
				count += k3 + 1 - a - 1
(a, b) = l[-1]
if a == b:
	count += 1
print(count)


n = int(input())
p = (0, 0)
p_d = 0
r = 1
for _ in range(n):
	a = tuple(map(int, input().split()))
	t_min = max(p[0], p[1])
	t_max = min(a[0], a[1])
	if t_max >= t_min:
		r += t_max - t_min + 1 - (1 if t_min == p_d else 0)
	p = a
	p_d = t_max
print(r)


n = input()
a = []
b = []
n = int(n)
c = []
for i in range(n):
	c.append(input().split())
for i in range(n):
	c[i][0] = int(c[i][0])
	c[i][1] = int(c[i][1])
temp_a = 0
temp_b = 0
output = 0
flag = 0
d = [[0, 0]]
for i in range(len(c)):
	if c[i] != d[len(d) - 1]:
		d.append(c[i])
for i in range(len(d) - 1):
	x = min(d[i + 1][0], d[i + 1][1])
	y = max(d[i][0], d[i][1])
	xx = max(d[i][0], d[i][1])
	if x >= y:
		output = output + x - y + 1
		if d[i + 1][0] == d[i + 1][1]:
			output = output - 1
if d[len(d) - 1][0] == d[len(d) - 1][1]:
	output = output + 1
print(output)


t = int(input())
x = [0]
y = [0]
x2 = 0
y2 = 0
ans = 0
k = t
n = 1
while t:
	a = input()
	(x1, y1) = (int(a.split()[0]), int(a.split()[1]))
	if x1 == x2 == y1 == y2 and t != k:
		k = t
	else:
		x.append(x1)
		y.append(y1)
		n = n + 1
	x2 = x1
	y2 = y1
	t = t - 1
for i in range(1, n):
	if min(x[i], y[i]) >= max(x[i - 1], y[i - 1]):
		ans = ans + min(x[i], y[i]) - max(x[i - 1], y[i - 1]) + 1
	if x[i - 1] == y[i - 1] and min(x[i], y[i]) >= max(x[i - 1], y[i - 1]) and (i != 1):
		ans = ans - 1
print(ans)


n = int(input())
sub = [0]
tot = [0]
for i in range(n):
	(a, b) = map(int, input().split())
	sub.append(a - b)
	tot.append(a + b)
ans = 1
for i in range(n):
	if tot[i] == tot[i + 1]:
		continue
	if tot[i + 1] - tot[i] >= abs(sub[i]) + abs(sub[i + 1]):
		ans += (tot[i + 1] - tot[i] - abs(sub[i]) - abs(sub[i + 1])) // 2 + 1
	if sub[i] == 0:
		ans -= 1
print(ans)


n = int(input())
lis = [tuple(map(int, input().split())) for _ in range(n)]
lis = list(set(lis))
lis.sort()
delo = 0
sol = 0
for i in lis:
	(x, y) = i
	sol += max(min(x, y) + 1 - delo, 0)
	delo = max(x, y) + 1 if x == y else max(x, y)
print(sol)


n = int(input())
scores = []
for i in range(n):
	scores.append([int(s) for s in input().split(' ')])
draws = 1 + min(scores[0])
for i in range(n - 1):
	(a1, b1) = scores[i]
	(a2, b2) = scores[i + 1]
	low = max(a1, b1)
	high = min(a2, b2)
	draws += max(0, high - low + 1) - int(a1 == b1)
print(draws)


def intersect(s1, s2):
	(x1, x2) = (s1[0], s1[1])
	(y1, y2) = (s2[0], s2[1])
	if x2 < y1 or y2 < x1:
		return 0
	else:
		t1 = max(x1, y1)
		t2 = min(x2, y2)
		if x1 == y1:
			return abs(t2 - t1)
		else:
			return abs(t2 - t1) + 1

def twopoints_draw(p1, p2):
	(x1, y1) = (p1[0], p1[1])
	(x2, y2) = (p2[0], p2[1])
	if x1 == x2 and y1 == y2:
		return 0
	else:
		return intersect([x1, x2], [y1, y2])
n = int(input())
a = [[0, 0] for i in range(n)]
for i in range(n):
	tp = [int(x) for x in input().split()]
	(a[i][0], a[i][1]) = (tp[0], tp[1])
p0 = [0, 0]
ans = twopoints_draw(p0, a[0])
for i in range(n - 1):
	ans += twopoints_draw(a[i], a[i + 1])
print(ans + 1)


def main():
	res = 0
	n = int(input())
	last = [0, 0]
	for i in range(0, n):
		x = list(map(int, input().split()))
		res += max(0, min(x[0], x[1]) - max(last[0], last[1]) + 1)
		if last[0] == last[1]:
			res -= 1
		last = x
	return res + 1
print(main())


n = int(input())
count = 1
(a, b) = (0, 0)
for i in range(n):
	(c, d) = map(int, input().split(' '))
	if min(c, d) >= max(a, b):
		if a != b:
			count += min(c, d) - max(a, b) + 1
		else:
			count += min(c, d) - max(a, b)
	(a, b) = (c, d)
print(count)


n = int(input())
pair = [None] * n
count = 0
add = 0
for i in range(n):
	pair[i] = list(map(int, input().split()))
if min(pair[0]) != 0:
	add += min(pair[0]) + 1
else:
	add += 1
count += add
for i in range(1, n):
	if min(pair[i]) - max(pair[i - 1]) < 0:
		continue
	add = max(min(pair[i]) - max(pair[i - 1]) + 1, 0)
	if pair[i - 1][0] == pair[i - 1][1]:
		add -= 1
	count += add
print(count)


N = int(input())
md = 1
(a1, b1) = (0, 0)

def count(a1, b1, a2, b2):
	if b1 > a2 or a1 > b2:
		return 0
	elif a1 == b1:
		return min(a2, b2) - a1
	elif a1 > b1:
		return min(a2, b2) - a1 + 1
	else:
		return min(a2, b2) - b1 + 1
for n in range(N):
	(a2, b2) = map(int, input().split())
	md += count(a1, b1, a2, b2)
	(a1, b1) = (a2, b2)
print(md)


n = int(input())
S = [[0, 0]] + [list(map(int, input().split())) for i in range(n)]

def score(i, j, k, l):
	if k == l:
		return score(i, j, k, l - 1)
	return max(0, min(k, l) - max(i - 1, j - 1))
ANS = 0
for i in range(1, n + 1):
	ANS += score(S[i - 1][0], S[i - 1][1], S[i][0], S[i][1])
if S[-1][0] == S[-1][1]:
	ANS += 1
print(ANS)


n = int(input())
l1 = []
l2 = []
for i in range(n):
	(a, b) = map(int, input().split())
	l1.append(a)
	l2.append(b)
ans = min(l1[0], l2[0]) + 1
for i in range(1, n):
	if max(l1[i - 1], l2[i - 1]) <= min(l1[i], l2[i]):
		if l1[i] != l1[i - 1] or l2[i] != l2[i - 1]:
			ans = ans - max(l1[i - 1], l2[i - 1]) + min(l1[i], l2[i]) + int(l1[i - 1] != l2[i - 1])
print(ans)


from sys import stdin, stdout
for _ in range(1):
	n = int(stdin.readline())
	x = y = 0
	ans = 1
	for i in range(n):
		(x1, y1) = list(map(int, stdin.readline().split()))
		ans += max(0, min(x1, y1) - max(x, y) + 1) - int(x == y)
		(x, y) = (x1, y1)
	print(ans)


n = int(input())
data = []
ans = 0
for i in range(n):
	(a, b) = list(map(int, input().split()))
	data.append([a, b])
ans += min(data[0]) + 1
for i in range(n - 1):
	if data[i][0] == data[i][1]:
		ans += min(data[i + 1]) - max(data[i])
	elif min(data[i + 1]) > 0:
		ans += max(min(data[i + 1]) - max(data[i]) + 1, 0)
print(ans)


a = b = 0
ans = 1
for i in range(int(input())):
	(a1, b1) = map(int, input().split())
	ans += max(0, min(a1, b1) - max(a, b) + bool(a != b))
	(a, b) = (a1, b1)
print(ans)


(m, n) = (0, 0)
s = 1
k = 0
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	if m == n and a == b and (a == m):
		continue
	elif m == n:
		s += min(a - m, b - n)
	elif m > n:
		if b - n >= m - n:
			t = m - n
			s += 1
			s += min(a - m, b - n - t)
	elif a - m >= n - m:
		t = n - m
		s += min(a - m - t, b - n)
		s += 1
	(m, n) = (a, b)
print(s)


n = int(input())
t = [(0, 0)]
for _ in range(n):
	(ai, bi) = map(int, input().split())
	t.append((ai, bi))
res = 0
i = 0
j = 0
while i <= n:
	(xi, yi) = t[i]
	while min(xi, yi) >= j:
		j += 1
		res += 1
	j = max(j, max(xi, yi))
	i += 1
print(res)


n = int(input())
count = 1
last = 0
pa = 0
pb = 0
for i in range(n):
	line = input().split(' ')
	a = int(line[0])
	b = int(line[1])
	if a >= pb and pa <= b:
		start = max(pa, pb)
		end = min(a, b)
		if end > last:
			start = max(last + 1, start)
			delta = end - start + 1
			count += delta
			last = end
	pa = a
	pb = b
print(count)


from sys import stdin

def intersection(first, a, second, b):
	diff = min(a, b) - max(first, second) + 1
	if diff < 0:
		return 0
	else:
		return diff
n = int(stdin.readline().strip())
win = 1
first = 0
second = 0
for _ in range(n):
	(a, b) = list(map(int, stdin.readline().split()))
	if first < second:
		first += 1
	else:
		second += 1
	win += intersection(first, a, second, b)
	first = a
	second = b
print(win)


n = int(input())
res = 1
t1 = 0
t2 = 0
for i in range(n):
	(new_t1, new_t2) = 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 = [(0, 0)]
pre = (0, 0)
res = 1
for i in range(n):
	cur = tuple(map(int, input().split(' ')))
	mn = max(pre[0], pre[1])
	mx = min(cur[0], cur[1])
	temp = mx - mn + 1
	if temp > 0:
		res += temp
	if pre[0] == pre[1] and mn == pre[0]:
		res -= 1
	pre = cur
print(res)


import sys
input = sys.stdin.readline
n = int(input())
(aa, bb) = (0, 0)
cnt = 1
for i in range(n):
	(a, b) = map(int, input().split())
	cnt += max(0, min(a, b) - max(aa, bb) + int(aa != bb))
	aa = a
	bb = b
print(cnt)


n = int(input())
a = b = 0
count = 0
m = 0
for i in range(n):
	(l1, l2) = map(int, input().split())
	if l1 == l2 and i != n - 1:
		m += 1
	aa = max(a, b)
	bb = min(l1, l2)
	if bb - aa >= 0:
		count += bb - aa + 1
	a = l1
	b = l2
print(count - m)


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())
draw = 1
state = 0
a = 0
for i in range(n):
	(b1, b2) = [int(j) for j in input().split()]
	minb = min(b1, b2)
	maxb = max(b1, b2)
	if state == 0:
		draw += minb - a
	else:
		draw += max(0, minb - a + 1)
	if minb == maxb:
		state = 0
	else:
		state = 1
	a = maxb
print(draw)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n,) = I()
lp = sp = 0
ans = 1
for i in range(n):
	(x, y) = I()
	if x + y == 0:
		continue
	k = min(x, y)
	l = max(lp, sp)
	ans += max(0, k - l + (lp != sp))
	(lp, sp) = (x, y)
print(ans)


lan = int(input())
t1 = list()
t2 = list()
rs = 0
truoc = 0
sau = 0
chen = False
for i in range(lan):
	(a, b) = map(int, input().split())
	if i == 0 and (a != 0 or b != 0):
		t1.append(0)
		t2.append(0)
		chen = True
	t1.append(a)
	t2.append(b)
if chen:
	lan += 1
cu = -1
for i in range(1, lan):
	truoc = t1[i - 1] if t1[i - 1] >= t2[i - 1] else t2[i - 1]
	sau = t2[i] if t2[i] <= t1[i] else t1[i]
	if sau < truoc:
		cu = sau
		continue
	rs += sau - truoc + 1
	if cu == truoc:
		rs -= 1
	cu = sau
if lan == 1 and a == 0 and (b == 0):
	rs = 1
print(rs)


import math
import sys
import collections

def getdict(n):
	d = {}
	if type(n) is list:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)
n = ii()
y = r = 1
for i in range(n):
	(a, b) = mi()
	x = min(a, b)
	r += max(x - y + 1, 0)
	y = max(a, b) + (a == b)
print(r)


n = int(input())
last = 0
total = 1
for _ in range(n):
	(a, b) = 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())
ans = 0
a = 0
b = 0
for _ in range(n):
	(l, r) = map(int, input().split())
	ans += max(0, min(l, r) - max(a, b) + 1)
	a = l
	b = r
	if a == b:
		a += 1
		b += 1
print(ans)


n = int(input())
(x, y) = (0, 0)
ans = 1
for i in range(n):
	if x == y:
		ans -= 1
	t = max(x, y)
	(x, y) = map(int, input().split())
	t = min(x, y) - t + 1
	ans += max(0, t)
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
	elif min(a, b) >= pa:
		ans += min(a, b) - pa + 1
	(pa, pb) = (a, b)
print(ans)


ans = 1
pre = [0, 0]
n = int(input())
for i in range(n):
	(x, y) = map(int, input().split())
	x1 = pre[0]
	y1 = pre[1]
	if x1 != y1:
		ans += max(0, min(x, y) - max(x1, y1) + 1)
	else:
		ans += max(0, min(x, y) - max(x1, y1))
	pre = [x, y]
print(ans)


t = int(input())
c = 1
(a, b) = (0, 0)
while t > 0:
	(ia, ib) = map(int, input().split())
	c += max(min(ia, ib) - max(a, b) + (a != b), 0)
	(a, b) = (ia, ib)
	t -= 1
print(c)


n = int(input())
score = [list(map(int, input().split())) for i in range(n)]
sCount = 0
lasts = 2
if score[0][0] == score[0][1]:
	sCount = score[0][0] + 1
	lasts = 2
else:
	sCount = min(score[0]) + 1
	if score[0][0] > score[0][1]:
		lasts = 0
	elif score[0][0] < score[0][1]:
		lasts = 1
for i in range(1, n):
	if lasts == 2:
		if score[i][0] == score[i][1]:
			sCount += score[i][0] - score[i - 1][0]
			lasts = 2
		elif score[i][0] > score[i][1]:
			sCount += score[i][1] - score[i - 1][1]
			lasts = 0
		elif score[i][0] < score[i][1]:
			sCount += score[i][0] - score[i - 1][0]
			lasts = 1
	else:
		k = lasts
		if score[i][0] == score[i][1]:
			sCount += score[i][0] - score[i - 1][k] + 1
			lasts = 2
		elif score[i][0] > score[i][1]:
			sCount += score[i][1] - score[i - 1][k] + 1 if score[i][1] - score[i - 1][k] >= 0 else 0
			lasts = 0
		elif score[i][0] < score[i][1]:
			sCount += score[i][0] - score[i - 1][k] + 1 if score[i][0] - score[i - 1][k] >= 0 else 0
			lasts = 1
print(sCount)


n = int(input())
a = [0]
b = [0]
draws = 1
last = 0
for k in range(n):
	(x, y) = [int(s) for s in input().split()]
	if x != a[-1] or y != b[-1]:
		a.append(x)
		b.append(y)
for i in range(1, len(a)):
	x = max(a[i - 1], b[i - 1], last + 1)
	y = min(a[i], b[i])
	if y - x >= 0:
		draws += y - x + 1
		if y > last:
			last = y
print(draws)


N = int(input())
items = []
for i in range(N):
	items.append(tuple(map(int, input().split())))
cnt = 1
pre_a = 0
pre_b = 0
for (a, b) in items:
	if a == pre_a and b == pre_b:
		continue
	elif a == pre_a and b != pre_b:
		if pre_b < pre_a and b >= a:
			cnt += 1
	elif a != pre_a and b == pre_b:
		if pre_a < pre_b and a >= b:
			cnt += 1
	elif pre_a == pre_b:
		cnt += min(a, b) - pre_a
	elif min(a, b) - max(pre_a, pre_b) >= 0:
		cnt += min(a, b) - max(pre_a, pre_b) + 1
	pre_b = b
	pre_a = a
print(cnt)


n = int(input())
flag = 0
(x, y) = list(map(int, input().split()))
count = min(x, y) + 1
(px, py) = (x, y)
if x > y:
	flag = 1
elif y > x:
	flag = 2
for i in range(1, n):
	(x, y) = list(map(int, input().split()))
	if x == y:
		if flag == 0:
			count += x - px
		elif flag == 1:
			count += x - px + 1
		else:
			count += y - py + 1
		flag = 0
	elif x > y:
		if flag == 0:
			count += y - py
		elif flag == 1:
			if y >= px:
				count += y - px + 1
		else:
			count += y - py + 1
		flag = 1
	else:
		if flag == 0:
			count += x - px
		elif flag == 1:
			count += x - px + 1
		elif x >= py:
			count += x - py + 1
		flag = 2
	px = x
	py = y
print(count)


n = int(input())
(ap, bp) = map(int, input().split())
dr = 1 + min(ap, bp)
for i in range(n - 1):
	(a, b) = map(int, input().split())
	l = min(a, b)
	h = max(ap, bp)
	dr += max(l - h, 0) + {True: {True: 0, False: 1}[ap == bp], False: 0}[l - h >= 0]
	ap = a
	bp = b
print(dr)


n = int(input())
scores = []
for _ in range(n):
	scores.append(list(map(int, input().split())))
last = [0, 0]
res = 1
for now in scores:
	if now[1] < last[0] or now[0] < last[1]:
		last = now
		continue
	elif last[0] == last[1]:
		res += min(now[0] - last[0], now[1] - last[1])
		last = now
		continue
	else:
		res += min(now[0], now[1]) - max(last[0], last[1]) + 1
		last = now
print(res)


ans = 1
(prevx, prevy) = (0, 0)
s = set()
for ii in range(int(input())):
	(x, y) = map(int, input().split())
	ma = max(prevx, prevy)
	mi = min(x, y)
	if ma > mi:
		(prevx, prevy) = (x, y)
		continue
	else:
		if prevx == prevy:
			ans += mi - ma
		else:
			ans += mi - ma + 1
		(prevx, prevy) = (x, y)
print(ans)


def func(x, y, newx, newy):
	sma = min(x, y)
	big = max(x, y)
	ns = min(newx, newy)
	nb = max(newx, newy)
	if ns < big:
		return 0
	if ns < nb:
		return ns - big + 1
	return nb - big + 1

def main():
	count = int(input())
	x = 0
	y = 0
	tie = 1
	for _ in range(count):
		test = input().split()
		newx = int(test[0])
		newy = int(test[1])
		tie += func(x, y, newx, newy)
		if x == y:
			tie -= 1
		x = newx
		y = newy
	print(tie)
main()


def arr_2d(n):
	return [[int(x) for x in stdin.readline().split()] for i in range(n)]
from sys import stdin
n = int(input())
a = arr_2d(n)
ans = min(a[0]) + 1
for i in range(1, n):
	(mi, ma) = (min(a[i]), max(a[i - 1]))
	if a[i - 1][0] == a[i - 1][1]:
		ans += min(a[i]) - a[i - 1][1]
	elif a[i - 1][0] < a[i - 1][1]:
		if a[i][0] > a[i][1]:
			ans += max(a[i][1] - a[i - 1][1] + 1, 0)
		else:
			ans += max(a[i][0] - a[i - 1][1] + 1, 0)
	elif a[i][0] < a[i][1]:
		ans += max(a[i][0] - a[i - 1][0] + 1, 0)
	else:
		ans += max(a[i][1] - a[i - 1][0] + 1, 0)
print(ans)


n = int(input())
l = [[0, 0]]
for i in range(n):
	l.append(list(map(int, input().split(' '))))
s = 0
change = True
for i in range(n):
	if change:
		s += max(min(l[i + 1]) - max(l[i]) + 1, 0)
	else:
		s += max(min(l[i + 1]) - max(l[i]), 0)
	if l[i + 1][0] == l[i + 1][1]:
		change = False
	else:
		change = True
print(s)


n = int(input())
count = 1
left = 0
right = 0
for i in range(0, n):
	s = list(map(int, input().split(' ')))
	if left > right:
		if s[1] >= left:
			count += min(s[0] - left, s[1] - left) + 1
	elif right > left:
		if s[0] >= right:
			count += min(s[0] - right, s[1] - right) + 1
	else:
		count += min(s[0] - left, s[1] - right)
	left = s[0]
	right = s[1]
print(count)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

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

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions
from collections import deque, defaultdict, OrderedDict
from fractions import Fraction
from decimal import Decimal

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(1):
		n = ri()
		pa = 0
		pb = 0
		ans = 1
		for i in range(n):
			(a, b) = ria()
			if min(a, b) - max(pa, pb) >= 0:
				ans += min(a, b) - max(pa, pb)
				if pa != pb:
					ans += 1
			pa = a
			pb = b
		print(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


n = int(input())
(ta, tb) = (0, 0)
cnt = 0
for i in range(n):
	(a, b) = map(int, input().split())
	cnt += max(0, -max(ta, tb) + min(a, b))
	if tb < ta and b >= ta or (ta < tb and a >= tb):
		cnt += 1
	(ta, tb) = (a, b)
print(cnt + 1)


n = int(input())
a = [0] * (n + 1)
b = [0] * (n + 1)
for i in range(n):
	(a[i], b[i]) = map(int, input().split())
cnt = 1
for i in range(n):
	if a[i] != a[i - 1] or b[i] != b[i - 1]:
		r = 0
		if a[i - 1] != b[i - 1]:
			r = 1
		p = a[i] - a[i - 1] + (b[i] - b[i - 1]) - (max(a[i], b[i]) - min(a[i - 1], b[i - 1])) + r
		cnt += max(0, p)
print(cnt)


n = int(input())
ina = 0
inb = 0
count = 1
for i in range(n):
	(a, b) = map(int, input().split())
	count += max(0, min(a, b) - max(ina, inb) + int(ina != inb))
	ina = a
	inb = b
print(count)


n = int(input())
a = [0] * (n + 1)
b = [0] * (n + 1)
ans = 1
a[0] = 0
b[0] = 0
an = 0
for i in range(1, n + 1):
	(a[i], b[i]) = map(int, input().split())
	if a[i - 1] == b[i - 1]:
		a[i - 1] += 1
	s = -max(a[i - 1], b[i - 1]) + min(a[i], b[i]) + 1
	s = max(0, s)
	ans += s
print(ans)


n = int(input())
x = 0
y = 0
total = 1
for _ in range(n):
	(a, b) = map(int, input().split())
	old_min = min(x, y)
	new_min = min(a, b)
	old_max = max(x, y)
	if old_min == old_max:
		total += new_min - old_max
	elif new_min >= old_max:
		total += new_min - old_max + 1
	(x, y) = (a, b)
print(total)


(a, b, draws) = (0, 0, 1)
for _ in range(int(input())):
	(A, B) = map(int, input().split())
	draws += max(0, min(A, B) - max(a, b) + (a != b))
	(a, b) = (A, B)
print(draws)


n = int(input())
s = [['*'] * 2] * (n + 1)
for i in range(1, n + 1):
	s[i] = list(map(int, input().split()))
count = 1
s[0] = [0, 0]
for i in range(1, n + 1):
	if s[i][0] + s[i][1] != 0:
		if min(s[i]) >= max(s[i - 1]) and s[i - 1][0] == s[i - 1][1]:
			count += min(s[i]) - max(s[i - 1])
		elif min(s[i]) >= max(s[i - 1]):
			count += min(s[i]) - max(s[i - 1]) + 1
print(count)


n = int(input())
(s1, s2) = (0, 0)
ans = 0
for i in range(n):
	(f1, f2) = list(map(int, input().split()))
	if s1 != f1 or s2 != f2:
		ans += max(0, min(f1, f2) - max(s1, s2) + 1)
		if s1 == s2:
			ans -= 1
	(s1, s2) = (f1, f2)
print(ans + 1)


n = int(input())
c = 1
(a, b) = (0, 0)
for i in range(n):
	(x, y) = map(int, input().split())
	f = 0 if a == b else 1
	if min(x, y) - max(a, b) + f >= 0:
		c += min(x, y) - max(a, b) + f
	(a, b) = (x, y)
print(c)


n = int(input())
l = []
ll = []
li = []
for i in range(n):
	(x, y) = map(int, input().split())
	l.append((x, y))
l = set(l)
l = list(l)
l.sort()
cou = 0
for i in range(len(l)):
	lol = 0
	if cou == 0:
		a = l[i][0]
	if a == l[i][0]:
		ll.append(l[i])
		cou = 1
		lol = 1
	else:
		lol = 1
		ll.sort(key=lambda x: x[1])
		li += ll
		a = l[i][0]
		ll = []
		ll.append(l[i])
ll.sort(key=lambda x: x[1])
li += ll
su = 0
li.insert(0, (0, 0))
if len(li) == 1:
	su += 1
for i in range(1, len(li)):
	a = li[i - 1]
	b = li[i]
	c = max(a[0], a[1])
	d = min(b[0], b[1])
	if d >= c:
		su += d - c + 1
for i in range(1, len(li) - 1):
	if li[i][0] == li[i][1]:
		su -= 1
print(su)


n = int(input())
a = []
for i in range(n):
	(x, y) = map(int, input().split())
	a.append((x, y))
ans = 0
ans = min(a[0][0], a[0][1]) + 1
for i in range(1, n):
	cury = a[i][1]
	curx = a[i][0]
	x = a[i - 1][0]
	y = a[i - 1][1]
	if x > y:
		if cury >= x:
			if cury < curx:
				ans += cury - x + 1
			else:
				ans += curx - x + 1
		else:
			continue
	elif x == y:
		if curx != x and cury != y:
			ans += min(cury - y, curx - x)
	elif curx >= y:
		if curx < cury:
			ans += curx - y + 1
		else:
			ans += cury - y + 1
	else:
		continue
print(ans)


n = int(input())
aa = 0
bb = 0
ans = 0
for tt in range(n):
	(a, b) = map(int, input().split())
	if a == b and tt < n - 1:
		continue
	m1 = max(aa, bb)
	m2 = min(a, b)
	ans += max(0, m2 - m1 + 1)
	aa = a
	bb = b
print(ans)


def parser():
	while 1:
		data = list(input().split(' '))
		for number in data:
			if len(number) > 0:
				yield number
input_parser = parser()

def get_word():
	global input_parser
	return next(input_parser)

def get_number():
	data = get_word()
	try:
		return int(data)
	except ValueError:
		return float(data)

def calc(pa, pb, a, b):
	p = max(pa, pb)
	n = min(a, b)
	if pa == pb and n - p > 0:
		return n - p
	elif not pa == pb and n - p >= 0:
		return 1 + n - p
	else:
		return 0
n = get_number()
paris = []
for i in range(n):
	a = get_number()
	b = get_number()
	paris.append([a, b])
lD = 0
draws = 1
lpari = [0, 0]
t = True
for pari in paris:
	draws += calc(lpari[0], lpari[1], pari[0], pari[1])
	lpari = pari
print(draws)


def fun(c, n):
	return max(0, min(min(n[0] - c[1], n[1] - c[0]), min(n[0] - c[0], n[1] - c[1])) + 1)
N = int(input())
current = [0, 0]
count = 1
for i in range(N):
	nxt = [int(j) for j in input().split(' ')]
	count -= current[0] == current[1]
	count += fun(current, nxt)
	current = nxt
print(count)


from math import *
from itertools import *
from collections import *
from bisect import *
n = int(input())
pa = pb = 0
result = 1
for _ in range(n):
	(a, b) = map(int, input().split())
	if pa == pb:
		result += max(0, min(a, b) - max(pa, pb))
	else:
		result += max(0, min(a, b) - max(pa, pb) + 1)
	(pa, pb) = (a, b)
print(result)


n = int(input())
scores = [[0, 0]]
for i in range(n):
	tm = [int(_) for _ in input().split(' ')]
	scores.append(tm)
(a, b) = (0, 0)
res = 1
for i in range(1, n + 1):
	res += max(0, min(scores[i]) - max(a, b) + 1)
	if a == b:
		res -= 1
	(a, b) = scores[i]
print(res)


n = int(input())
(a, b) = map(int, input().split())
ans = min(a, b) + 1
prev_a = a
prev_b = b
max_n = min(a, b)
for i in range(n - 1):
	(a, b) = map(int, input().split())
	l = max(prev_a, prev_b)
	r = min(a, b)
	ans += max(r - l + 1, 0)
	if r >= l and max_n == l:
		ans -= 1
	max_n = r
	prev_a = a
	prev_b = b
print(ans)


t = int(input())
tiecount = 1
prev = (0, 0)
curr = (0, 0)
for i in range(0, t):
	prev = curr
	inp = input().split(' ')
	curr = (int(inp[0]), int(inp[1]))
	if curr != prev:
		if prev[0] == prev[1]:
			tiecount = tiecount + (min(curr) - prev[1])
		else:
			x = min(curr) - max(prev)
			if x >= 0:
				tiecount = tiecount + x + 1
print(tiecount)


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


n = int(input())
prev1 = 1
prev2 = 1
ans = 1
for i in range(n):
	ab = list(map(int, input().split()))
	if ab[1] >= prev1 and ab[0] >= prev2:
		ans += min(ab[0], ab[1]) - max(prev1, prev2) + 1
		if ab[1] < ab[0]:
			prev1 = ab[0]
			prev2 = ab[1] + 1
		else:
			prev1 = ab[0] + 1
			prev2 = ab[1]
	else:
		prev1 = max(ab[0], prev1)
		prev2 = max(ab[1], prev2)
print(ans)


(u, v, ans) = (0, 0, 1)
n = int(input())
for _ in range(n):
	(p, q) = map(int, input().split())
	k = 0 if u == v else 1
	ans += max(min(p, q) - max(u, v) + k, 0)
	(u, v) = (p, q)
print(ans)


n = int(input())
(ans, a, b, x, y, c) = (0, 0, 0, 0, 0, 0)
for i in range(n):
	(x, y) = (a, b)
	(a, b) = [int(x) for x in input().split(' ')]
	if a + b == 0:
		continue
	if x == y:
		ans += max(0, min(a, b) - max(x, y))
		if x + y == 0 and a + b != 0:
			ans += 1
	else:
		ans += max(0, min(a, b) - max(x, y) + 1)
if ans == 0:
	ans = 1
print(ans)


c = 1
la = 0
lb = 0
p = {}
p[0] = 1
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	t1 = min(a, b)
	t2 = max(la, lb)
	if t1 - t2 > 0:
		c = c + (t1 - t2)
		try:
			k = p[t2]
		except:
			c += 1
		p[t2] = 1
		p[t1] = 1
	elif t1 - t2 == 0:
		try:
			k = p[t1]
		except:
			p[t1] = 1
			c += 1
	(la, lb) = (a, b)
print(c)


n = int(input())
c = 0
d = 0
ans = 1
for i in range(n):
	(a, b) = list(map(int, input().split()))
	if c < d:
		if a < d:
			pass
		else:
			ans += min(a - d, b - d) + 1
	elif c == d:
		ans += min(a - c, b - d)
	elif b < c:
		pass
	else:
		ans += min(b - c, a - c) + 1
	c = a
	d = b
print(ans)


n = int(input())
(x, y) = (0, 0)
ans = 1
for _ in range(n):
	(next_x, next_y) = [int(i) for i in input().split()]
	l = max(x, y)
	r = min(next_x, next_y)
	ans += max(0, r - l + (x != y))
	(x, y) = (next_x, next_y)
print(ans)


def solve():
	number_of_score_records = int(input())
	last_score_a = 0
	last_score_b = 0
	draw_times = 1
	for _ in range(number_of_score_records):
		(score_a, score_b) = map(int, input().split())
		range_a = [last_score_a, score_a]
		range_b = [last_score_b, score_b]
		if has_score_change(range_a, range_b) and are_intersected(range_a, range_b):
			if last_score_a != last_score_b:
				draw_times += 1
			draw_times += get_instersection_size(range_a, range_b)
		(last_score_a, last_score_b) = (score_a, score_b)
	print(draw_times)

def has_score_change(range_a, range_b):
	return range_a[0] != range_a[1] or range_b[0] != range_b[1]

def are_intersected(range_a, range_b):
	return range_a[0] <= range_b[1] and range_b[0] <= range_a[1]

def get_instersection_size(range_a, range_b):
	return abs(max(range_a[0], range_b[0]) - min(range_a[1], range_b[1]))
solve()


import sys
import math
import collections
from pprint import pprint as pp
mod = 1000000007
MAX = 10 ** 10

def array():
	return list(map(int, input().split()))

def vector(size, val=0):
	vec = [val for i in range(size)]
	return vec

def matrix(rowNum, colNum, val=0):
	mat = []
	for i in range(rowNum):
		collumn = [val for j in range(colNum)]
		mat.append(collumn)
	return mat
n = int(input())
a = [[0, 0]]
for i in range(n):
	a.append(array())
ans = 0
for i in range(1, n + 1):
	ans += max(0, min(a[i]) - max(a[i - 1]) + 1)
	if a[i][0] == a[i][1]:
		a[i][0] = a[i][1] = a[i][0] + 1
print(ans)


amount = int(input())
array = [[0, 0]]
for i in range(amount):
	array.append([int(s) for s in input().split()])
ans = 0
counter = 0
for i in range(1, len(array)):
	ans += max(0, min(array[i]) - max(array[i - 1]) + 1)
	if array[i][0] == array[i][1] and i < len(array) - 1:
		counter += 1
print(ans - counter)


n = int(input())
(a, b) = (0, 0)
ans = 1
for _ in range(n):
	(p, q) = map(int, input().split())
	temp = 0
	if a != b:
		temp = 1
	if min(p, q) - max(a, b) + temp >= 0:
		ans += min(p, q) - max(a, b) + temp
	(a, b) = (p, q)
print(ans)


n = int(input())
count = 0
(a_prev, b_prev) = (-1, 0)
for _ in range(n):
	(a, b) = map(int, input().split())
	start = max(a_prev, b_prev)
	end = min(a, b)
	if end >= start:
		count += end - start + 1
		if a_prev == b_prev:
			count -= 1
	(a_prev, b_prev) = (a, b)
print(count)


import math
import bisect
import itertools
import sys
I = lambda : sys.stdin.readline()
mod = 10 ** 9 + 7

def modu(a, m):
	if a % m:
		return a % m
	return m

def mindiff(a):
	b = a[:]
	b.sort()
	m = 10000000000
	for i in range(len(b) - 1):
		if b[i + 1] - b[i] < m:
			m = b[i + 1] - b[i]
	return m

def lcm(a, b):
	return a * b // math.gcd(a, b)

def merge(a, b):
	i = 0
	j = 0
	c = 0
	ans = []
	while i < len(a) and j < len(b):
		if a[i] < b[j]:
			ans.append(a[i])
			i += 1
		else:
			ans.append(b[j])
			c += len(a) - i
			j += 1
	ans += a[i:]
	ans += b[j:]
	return (ans, c)

def mergesort(a):
	if len(a) == 1:
		return (a, 0)
	mid = len(a) // 2
	(left, left_inversion) = mergesort(a[:mid])
	(right, right_inversion) = mergesort(a[mid:])
	(m, c) = merge(left, right)
	c += left_inversion + right_inversion
	return (m, c)

def is_prime(num):
	if num == 1:
		return False
	if num == 2:
		return True
	if num == 3:
		return True
	if num % 2 == 0:
		return False
	if num % 3 == 0:
		return False
	t = 5
	a = 2
	while t <= int(math.sqrt(num)):
		if num % t == 0:
			return False
		t += a
		a = 6 - a
	return True

def ceil(a, b):
	if a % b == 0:
		return a // b
	else:
		return a // b + 1

def ncr1(n, r):
	s = 1
	for i in range(min(n - r, r)):
		s *= n - i
		s %= mod
		s *= pow(i + 1, mod - 2, mod)
		s %= mod
	return s
n = int(input())
(x, y) = map(int, input().split())
ans = min(x, y) + 1
for i in range(n - 1):
	(x1, y1) = map(int, input().split())
	if x != y:
		ans += max(min(x1, y1) - max(x, y), -1) + 1
	else:
		ans += max(min(x1, y1) - max(x, y), -1)
	x = x1
	y = y1
print(ans)


import sys
try:
	sys.stdin = open('in.in', 'r')
	sys.stdout = open('out.out', 'w')
except:
	pass
n = int(input())
(ca, cb) = (0, 0)
ans = 1
reverse = 0
for ti in range(n):
	(va, vb) = map(int, input().split())
	if reverse:
		(va, vb) = (vb, va)
	if va == ca and vb == cb:
		continue
	if va == ca:
		if cb < ca and vb >= ca:
			ans += 1
		cb = vb
		continue
	if vb == cb:
		if ca < cb and va >= cb:
			ans += 1
		ca = va
		continue
	if ca > cb:
		(ca, cb) = (cb, ca)
		(va, vb) = (vb, va)
		reverse ^= 1
	if va < cb:
		ca = va
		cb = vb
		continue
	if ca != cb:
		ca = cb
		ans += 1
	minlim = min(va, vb)
	ans += minlim - ca
	ca = va
	cb = vb
print(ans)


n = int(input())
a = []
for i in range(n):
	a.append([int(x) for x in input().split()])
k = 1
k += min(a[0][0], a[0][1])
for i in range(0, n - 1):
	if not a[i + 1][0] == a[i + 1][1] == a[i][0] == a[i][1]:
		if min(a[i + 1][0], a[i + 1][1]) - max(a[i][0], a[i][1]) >= 0:
			if a[i][0] != a[i][1]:
				k += min(a[i + 1][0], a[i + 1][1]) - max(a[i][0], a[i][1]) + 1
			else:
				k += min(a[i + 1][0], a[i + 1][1]) - max(a[i][0], a[i][1])
print(k)


import sys
from collections import deque
import math
input_ = lambda : sys.stdin.readline().strip('\r\n')
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
li = lambda : list(input_())
pr = lambda x: print(x)
f = lambda : sys.stdout.flush()
n = ii()
ans = 1
p1 = p2 = 0
for i in range(n):
	(x, y) = il()
	ans += max(0, min(x, y) - max(p1, p2) + 1)
	if p1 == p2 and ans > 0:
		ans -= 1
	(p1, p2) = (x, y)
print(ans)


n = int(input())
scores = [(0, 0)]
for _ in range(n):
	(a, b) = map(int, input().split())
	if (a, b) != scores[-1]:
		scores.append((a, b))
num_ties = 0
for i in range(len(scores) - 1):
	(al, bl) = scores[i]
	(ah, bh) = scores[i + 1]
	min_tie = max(al, bl)
	max_tie = min(ah, bh)
	if min_tie <= max_tie:
		num_ties += max_tie - min_tie
		if ah != bh:
			num_ties += 1
(ae, be) = scores[-1]
if ae == be:
	num_ties += 1
print(num_ties)


x = 0
y = 0
ans = 1
n = int(input())
a = [0] * (n + 1)
b = [0] * (n + 1)
a[0] = 0
b[0] = 0
for i in range(n):
	(x, y) = map(int, input().split())
	a[i] = x
	b[i] = y
	if a[i] < b[i - 1] or b[i] < a[i - 1]:
		continue
	else:
		tmp = min(a[i], b[i]) - max(a[i - 1], b[i - 1])
		if a[i - 1] != b[i - 1]:
			tmp += 1
		ans += tmp
print(ans)


n = int(input())
a = []
b = []
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
ans = 1
prev_a = 0
prev_b = 0
for i in range(0, len(a)):
	if a[i] < prev_b and b[i] < prev_a:
		prev_a = a[i]
		prev_b = b[i]
		continue
	if max(prev_a, prev_b) <= min(a[i], b[i]):
		ans += min(a[i], b[i]) - max(prev_a, prev_b) + 1
	if prev_a == prev_b:
		ans -= 1
	prev_a = a[i]
	prev_b = b[i]
print(ans)


preva = 0
prevb = 0
ans = 1
for _ in range(int(input())):
	(a, b) = [int(x) for x in input().split()]
	if a == preva and prevb == b:
		continue
	elif preva == prevb:
		ans += min(a, b) - prevb
	else:
		ans += max(0, min(a, b) - max(preva, prevb) + 1)
	preva = a
	prevb = b
print(ans)


n = int(input())
goals = []
for _ in range(n):
	(a, b) = [int(x) for x in input().split()]
	goals.append((a, b))
if goals[0] != (0, 0):
	goals = [(0, 0)] + goals
cnt = 1
last = 0
for i in range(len(goals) - 1):
	cur = goals[i]
	next = goals[i + 1]
	if cur == next:
		continue
	maxcurdiag = max(cur)
	minnextdiag = min(next)
	if minnextdiag >= maxcurdiag:
		cnt += minnextdiag - maxcurdiag + 1
	if maxcurdiag == last:
		cnt -= 1
	last = minnextdiag
print(cnt)


n = int(input().strip())
draws = 1
scores = [(0, 0)]
for time in range(n):
	(a, b) = [int(j) for j in input().strip().split(' ')]
	(c, d) = scores[-1]
	difference = min(a, b) - max(c, d)
	draws += difference + 1 if difference >= 0 else 0
	if c == d:
		draws -= 1
	scores += [(a, b)]
print(draws)


(t,) = map(int, input().split())
pairs = []
for i in range(0, t):
	(a, b) = map(int, input().split())
	pairs.append((a, b))
pre = (0, 0)
draw = 1
for p in pairs:
	mx = max(pre[0], pre[1])
	mn = min(p[0], p[1])
	draw += max(0, mn - mx)
	if pre[0] != pre[1] and mn >= mx:
		draw += 1
	pre = p
print(draw)


n = int(input())

def intersect(a, b, c, d):
	if c < a:
		(a, b, c, d) = (c, d, a, b)
	b -= a
	c -= a
	d -= a
	a -= a
	if d <= b:
		return d - c + 1
	return b - c + 1
(a, b) = (0, 0)
ans = 1
for _ in range(n):
	(na, nb) = map(int, input().split())
	peresech = intersect(a, na, b, nb)
	ans += max(0, peresech - (a == b))
	(a, b) = (na, nb)
print(ans)


def read_int():
	return int(input())

def read_ints():
	return map(int, input().split(' '))
n = read_int()
ans = 1
a = 0
b = 0
used = set([0])
for i in range(n):
	(ai, bi) = read_ints()
	hi = min(ai, bi)
	lo = max(a, b)
	if hi >= lo:
		ans += max(0, hi - lo - 1)
		if not hi in used:
			ans += 1
			used.add(hi)
		if not lo in used:
			ans += 1
			used.add(lo)
	(a, b) = (ai, bi)
print(ans)


from sys import stdin, stdout
input = stdin.readline

def main():
	n = int(input())
	preva = prevb = -1
	ans = 0
	for i in range(n):
		(a, b) = map(int, input().split())
		if preva != prevb:
			ans += max(0, min(a, b) - max(preva, prevb) + 1)
		else:
			ans += max(0, min(a, b) - max(preva, prevb))
		(preva, prevb) = (a, b)
	print(ans)
main()


ans = 0
n = int(input())
c = [[0, 0]]
for i in range(n):
	(a, b) = map(int, input().split())
	if c[-1][0] == a and c[-1][1] == b:
		x = 1
	else:
		c.append([a, b])
f = 1
for i in range(1, len(c)):
	f = 0
	if c[i][0] == c[i][1] and i != len(c) - 1:
		ans -= 1
	if max(c[i - 1][0], c[i - 1][1]) <= min(c[i][0], c[i][1]):
		ans += min(c[i][0], c[i][1]) - max(c[i - 1][0], c[i - 1][1]) + 1
print(ans + f)


n = int(input())
(a, b) = (0, 0)
leading = [False, False]
ans = 1
for i in range(n):
	(a1, b1) = [int(k) for k in input().split(' ')]
	if a1 != a or b1 != b:
		p = min(a1, b1) - max(a, b)
		if p >= 0:
			ans += p
			if a != b:
				ans += 1
	(a, b) = (a1, b1)
print(ans)


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)


n = int(input())
res = 0
prev = (0, 0)
res += 1

def f(r1, r2):
	if r1[1] < r2[0]:
		return 0
	if r2[1] < r1[0]:
		return 0
	if r1[0] <= r2[0] and r1[1] >= r2[1]:
		return r2[1] - r2[0] + 1
	if r2[0] <= r1[0] and r2[1] >= r1[1]:
		return r1[1] - r1[0] + 1
	a = sorted([*r1, *r2])
	return a[2] - a[1] + 1
for _ in range(n):
	(a, b) = map(int, input().split())
	c = f((prev[0], a), (prev[1], b))
	res += c
	if prev[0] == prev[1]:
		res -= 1
	prev = (a, b)
print(res)


def football_problem():
	n = eval(input())
	answer = 1
	(a, b) = (0, 0)
	while n > 0:
		n -= 1
		(x, y) = input().split(' ')
		(x, y) = (eval(x), eval(y))
		if x == a and y == b:
			continue
		answer += max(0, min(x, y) - max(a, b) + 1)
		if a == b:
			answer -= 1
		(a, b) = (x, y)
	print(answer)
	return
football_problem()


R = lambda : map(int, input().rstrip().split())
n = int(input())
ans = 1
(l, r) = (0, 0)
for _ in range(1, n + 1):
	(x, y) = R()
	ans += max(0, min(x, y) - max(l, r) + (1 if l != r else 0))
	(l, r) = (x, y)
print(ans)


def Find(a, b, c, d):
	return min(a, b) - max(c, d) + (c != d)
n = int(input())
answer = 1
prevA = 0
prevB = 0
for i in range(0, n):
	(a, b) = map(int, input().split())
	if a >= prevA and b >= prevB:
		answer += max(0, Find(a, b, prevA, prevB))
	prevA = a
	prevB = b
print(answer)


n = int(input())
d = 0
prev = (-1, -2)
out = 0
for i in range(n):
	(a, b) = list(map(int, input().split()))
	if (a, b) == prev:
		continue
	diff = min(a, b) - d
	if diff >= 0:
		if prev[0] == prev[1]:
			out -= 1
		out += diff + 1
		prev = (a, b)
	d = max(a, b)
print(out)


n = int(input())
(t1, t2) = (0, 0)
res = 1
for i in range(n):
	(t3, t4) = map(int, input().split())
	l = max(t1, t2)
	r = min(t3, t4)
	if l <= r:
		if t1 == t2:
			res += abs(l - r)
		else:
			res += abs(r - l) + 1
	t1 = t3
	t2 = t4
print(res)


n = int(input())
k = 1
a1 = 0
b1 = 0
for i in range(n):
	(a2, b2) = map(int, input().split())
	c = min(a2, b2) - max(a1, b1)
	if (a1 - b1) * (a2 - b2) != 0:
		if c >= 0:
			k += c + 1
	elif a1 == b1:
		k += c
	else:
		k += c + 1
	(a1, b1) = (a2, b2)
print(k)


n = int(input())
a = [tuple(map(int, input().split())) for i in range(n)]
ans = 0
sol = set()
pv = 0
chi = 1
for (i, j) in a:
	(x, y) = (i, j)
	if chi == 0:
		(x, y) = (j, i)
	if y >= pv:
		if (pv, pv) not in sol:
			ans += 1
			sol.add((pv, pv))
		ans += min(x, y) - pv
		if i == j and (i, i) not in sol:
			sol.add((i, j))
	if i > j:
		pv = i
		chi = 1
	else:
		pv = j
		chi = 0
print(ans)


n = int(input())
(a, b) = map(int, input().split())
count = 1 + min(a, b)
q = [a, b]
for i in range(n - 1):
	z = q.index(max(q))
	if q[0] == q[1]:
		z = 2
	(a, b) = map(int, input().split())
	w = [a, b]
	e = w.index(max(w))
	if w[0] == w[1]:
		e = 2
	if z == 2:
		count += min(w[0] - q[0], w[1] - q[1])
	elif e == 2:
		w != q
		count += min(w) - max(q) + 1
	elif z == e:
		if min(w) >= max(q):
			count += min(w) - max(q) + 1
	else:
		count += min(w) - max(q) + 1
	q = [a, b]
print(count)


n = int(input())
(px, py) = (-1, -1)
ans = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	if not (a == px and b == py):
		l = max(px, py)
		u = min(a, b)
		if l <= u:
			ans += u - l
			if px != py:
				ans += 1
		(px, py) = (a, b)
print(ans)


n = int(input())
ll = 0
rr = 0
ans = 1
for i in range(n):
	[nl, nr] = list(map(int, input().split()))
	if min(nl, nr) >= max(ll, rr):
		ans += min(nl, nr) - max(ll, rr)
		if ll != rr:
			ans += 1
	ll = nl
	rr = nr
print(ans)


n = int(input())
cnt = 1
(last_a, last_b) = (0, 0)
for _ in range(n):
	(a, b) = map(int, input().split())
	if last_a == last_b:
		if a <= b:
			cnt += a - last_a
		else:
			cnt += b - last_b
	elif last_a > last_b:
		if a == b:
			cnt += b - last_a + 1
		elif a > b:
			if b >= last_a:
				cnt += b - last_a + 1
			else:
				cnt += 0
		else:
			cnt += a - last_a + 1
	elif a == b:
		cnt += a - last_b + 1
	elif b > a:
		if a >= last_b:
			cnt += a - last_b + 1
		else:
			cnt += 0
	else:
		cnt += b - last_b + 1
	last_a = a
	last_b = b
print(cnt)


dat = int(input().strip())
draw = 0
pa = -1
pb = 0
for x in range(dat):
	(a, b) = [int(x) for x in input().strip().split(' ')]
	if a == pa and b == pb:
		pa = a
		pb = b
		continue
	if pa < pb:
		draw += max(0, b + 1 - pb - max(0, b - a))
	elif pa > pb:
		draw += max(0, a + 1 - pa - max(0, a - b))
	else:
		draw += min(a, b) - pa
	pa = a
	pb = b
print(draw, end='')


def f(scores):
	count = 1
	x0 = 0
	y0 = 0
	for (x, y) in scores:
		if x == 0 and y == 0:
			continue
		mn = min(x, y)
		mx0 = max(x0, y0)
		if mn >= mx0:
			count += mn - mx0 + (1 if x0 != y0 else 0)
		x0 = x
		y0 = y
	return count
scores = []
n = int(input())
for i in range(n):
	scores.append(map(lambda _: int(_), input().split(' ')))
print(f(scores))


n = int(input())
count = 1
preva = 0
prevb = 0
for x in range(n):
	[a, b] = [int(i) for i in input().split()]
	if n == 1:
		print(min(a, b) + 1)
	else:
		if min(a, b) >= max(preva, prevb):
			count += min(a, b) - max(preva, prevb) + 1
		if preva == prevb:
			count -= 1
	preva = a
	prevb = b
if n != 1:
	print(count)


def count_evens(prev, curr):
	diff = min(curr) - max(prev)
	if diff < 0:
		return 0
	if prev[0] == prev[1]:
		diff -= 1
	return diff + 1
n = int(input())
logs = [tuple((int(x) for x in input().split())) for _ in range(n)]
prev_log = (0, 0)
even_counts = 1
for log in logs:
	if log == prev_log:
		continue
	even_counts += count_evens(prev_log, log)
	prev_log = log
print(even_counts)


n = int(input())
(ap, bp) = (0, 0)
mxp = -1
ans = 0
for _ in range(n):
	(a, b) = [int(x) for x in input().split()]
	mx = max(ap, bp)
	mn = min(a, b)
	if mx <= mn:
		ans += mn - mx + 1
		if mx == mxp:
			ans -= 1
		mxp = mn
	(ap, bp) = (a, b)
print(ans)


from sys import exit
inn = lambda : input().strip()
score = []
for _ in range(int(inn())):
	score.append(tuple(map(int, inn().split(' '))))
draw = 1
beforeA = beforeB = 0
for (nextA, nextB) in score:
	if beforeA == beforeB:
		draw += min(nextA, nextB) - beforeA
	if beforeA > beforeB:
		if nextB >= beforeA:
			draw += min(nextA, nextB) - beforeA + 1
	if beforeB > beforeA:
		if nextA >= beforeB:
			draw += min(nextA, nextB) - beforeB + 1
	(beforeA, beforeB) = (nextA, nextB)
print(draw)


n = int(input())
ans = 1
(a, b) = (0, 0)
check = [None] * n
for i in range(n):
	(c, d) = map(int, input().split())
	if c < d:
		check[i] = '<'
	elif c == d:
		check[i] = '='
	elif c > d:
		check[i] = '>'
	if i == 0:
		if c != 0 and d != 0:
			ans += min(c, d)
	elif check[i - 1] == '<':
		if check[i] == '<':
			if b <= c:
				ans += c - b + 1
		elif check[i] == '=':
			ans += c - b + 1
		elif check[i] == '>':
			ans += d - b + 1
	elif check[i - 1] == '=':
		if check[i] == '<':
			if a < c:
				ans += c - a
		elif check[i] == '=':
			if a < c:
				ans += c - a
		elif check[i] == '>':
			if b < d:
				ans += d - b
	elif check[i - 1] == '>':
		if check[i] == '<':
			ans += c - a + 1
		elif check[i] == '=':
			ans += c - a + 1
		elif check[i] == '>':
			if a <= d:
				ans += d - a + 1
	(a, b) = (c, d)
print(ans)


import sys

def rint():
	return map(int, sys.stdin.readline().split())
n = int(input())
a_prv = 0
b_prv = 0
cnt = 1
same_prv = 0
for i in range(n):
	(a, b) = rint()
	tmp = min(a, b) - max(a_prv, b_prv)
	if tmp >= 0:
		if same_prv != max(a_prv, b_prv):
			cnt += 1
		cnt += tmp
		same_prv = min(a, b)
	a_prv = a
	b_prv = b
print(cnt)


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()) - 1
(pa, pb) = map(int, input().split(' '))
ans = 1 + min(pa, pb)
for _ in range(n):
	(a, b) = map(int, input().split(' '))
	if pa > pb:
		if a < b:
			ans += 1 + (a - pa)
		if a > b:
			ans += max(0, 1 - pa + b)
		if a == b:
			ans += min(a - pa, b - pb) + 1
	if pb > pa:
		if a > b:
			ans += 1 + (b - pb)
		if a < b:
			ans += max(0, 1 - pb + a)
		if a == b:
			ans += min(a - pa, b - pb) + 1
	if pa == pb:
		if a > b:
			ans += b - pb
		if a < b:
			ans += a - pa
		if a == b:
			ans += a - pa
	(pa, pb) = (a, b)
print(ans)


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict
import math

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
n = intIn()
if n == 1:
	(a, b) = listIn()
	print(min(a, b) + 1)
else:
	li = [[0, 0]]
	for i in range(n):
		(a, b) = listIn()
		li.append([a, b])
	cnt = 0
	l = len(li)
	if l == 1:
		print(min(a, b) + 1)
		exit(0)
	for i in range(1, l):
		m1 = min(li[i])
		m2 = max(li[i - 1])
		if m1 >= m2:
			if li[i - 1][0] == li[i - 1][1]:
				cnt += m1 - m2
			else:
				cnt += m1 - m2 + 1
	print(cnt + 1)


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):
	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)


from collections import deque
import heapq
import math
import bisect

def main():
	N = int(input())
	N += 1
	(A, B) = ([0] * N, [0] * N)
	for i in range(N - 1):
		(A[i + 1], B[i + 1]) = map(int, input().split())
	ans = 1
	for i in range(N - 1):
		ans += max(0, min(A[i + 1], B[i + 1]) - max(A[i], B[i]) + (0 if A[i] == B[i] else 1))
	print(ans)
main()


n = int(input())
(ans, i) = (0, 0)
for _ in range(n):
	(x, y) = map(int, input().split())
	(m, M) = (min(x, y), max(x, y))
	if m >= i:
		ans += m - i + (M > m)
	i = M
ans += x == y
print(ans)


n = int(input())
(A, B) = zip(*[[int(i) for i in input().split()] for j in range(n)])
C = []
D = []
m = 0
for (i, a, b) in zip(range(n), A, B):
	if i != n - 1 and a == b:
		continue
	m += 1
	C.append(a)
	D.append(b)
A = C[::-1] + [0]
B = D[::-1] + [0]
ans = 0
for i in range(m):
	if i != m - 1 and A[i + 1] == A[i] and (B[i + 1] == B[i]):
		continue
	if A[i + 1] > B[i] or B[i + 1] > A[i]:
		continue
	ans += min(A[i], B[i]) - max(A[i + 1], B[i + 1]) + 1
print(ans)


import sys
import collections
from math import floor, ceil, log2
inp = lambda : sys.stdin.readline().strip()
n = int(inp())
a = []
j = 0
ans = 0
for i in range(n):
	(x, y) = map(int, inp().split())
	if x >= j and y >= j:
		ans += min(x, y) - j + 1
		j += min(x, y) - j + 1
	j = max(j, x, y)
print(ans)


n = int(input())
(o_x, o_y) = map(int, input().split())
ties = min(o_x, o_y) + 1
for _ in range(n - 1):
	if o_x == o_y:
		ties -= 1
	(x, y) = map(int, input().split())
	if x == o_x and y == o_y:
		if x == y:
			ties += 1
		continue
	ties += min(x, y) - max(o_x, o_y) + 1 if min(x, y) - max(o_x, o_y) + 1 > 0 else 0
	o_x = x
	o_y = y
print(ties)


import collections
import random
import heapq
import bisect
import math
import time

class Solution2:

	def solve(self, A1, A2):
		pass

def gcd(a, b):
	if not b:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return b * a // gcd(b, a)

class Solution:

	def solve(self, scores):
		out = 1
		a = 0
		b = 0
		for (x, y) in scores:
			out += max(min(x, y) - max(a, b) + int(a != b), 0)
			(a, b) = (x, y)
		return out
sol = Solution()
sol2 = Solution2()
for test_case in range(1):
	N = input()
	a = []
	for _ in range(int(N)):
		a.append([int(c) for c in input().split()])
	out = sol.solve(a)
	print(str(out))


n = int(input())
(a, b) = map(int, input().split())
c = min(a, b) + 1
z = a
x = b
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	c += min(a, b) - max(z, x) if min(a, b) - max(z, x) > -2 else -1
	if z != x:
		c += 1
	z = a
	x = b
print(c)


n = int(input())
ma = -1
ans = 0
(la, lb) = (-1, -1)
for _ in range(n):
	(a, b) = map(int, input().split())
	if [a, b] != [la, lb]:
		ans += max(0, min(a, b) - ma + 1 - (la == lb))
		ma = max(a, b)
		(la, lb) = (a, b)
print(ans)


(ans, oldx, oldy) = (1, 0, 0)
for i in range(int(input())):
	(x, y) = map(int, input().split())
	if oldx == oldy:
		z = 0
	else:
		z = 1
	ans += max(min(x, y) - max(oldx, oldy) + z, 0)
	(oldx, oldy) = (x, y)
print(ans)


import sys
import bisect
from random import randint
input = lambda : sys.stdin.readline().strip('\r\n')
from math import gcd
sa = lambda : input()
sb = lambda : int(input())
sc = lambda : input().split()
sd = lambda : list(map(int, input().split()))
se = lambda : float(input())
sf = lambda : list(input())
flsh = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7

def hnbhai():
	n = sb()
	(x, y) = (0, 0)
	ans = [[0, 0]]
	for i in range(n):
		(a, b) = sd()
		ans.append([a, b])
	t = 1
	for i in range(1, n + 1):
		t += max(0, min(ans[i][0], ans[i][1]) - max(ans[i - 1][0], ans[i - 1][1]) + 1)
		if t > 0 and ans[i - 1][0] == ans[i - 1][1]:
			t -= 1
	print(t)
for _ in range(1):
	hnbhai()


n = int(input())
old_scores = [0, 0]
ans = 1
for i in range(0, n):
	new_scores = [int(x) for x in input().split()]
	if min(new_scores) >= max(old_scores):
		ans += min(new_scores) - max(old_scores) + 1
		if old_scores[0] == old_scores[1]:
			ans -= 1
		old_scores = [max(old_scores), max(old_scores)]
	old_scores = new_scores
print(ans)


n = int(input())
(x, y) = (0, 0)
cnt = 1
for _ in range(n):
	(a, b) = list(map(int, input().split()))
	m1 = max(x, y)
	m2 = min(a, b)
	cnt += max(0, m2 - m1 + (x != y))
	(x, y) = (a, b)
print(cnt)


N = int(input())
ans = 1
prev1 = 0
prev2 = 0
for i in range(N):
	(a, b) = [int(s) for s in input().split(' ')]
	if prev1 < prev2:
		if a >= prev2:
			ans += 1
			prev1 = prev2
			ans += min(a, b) - prev1
	elif prev1 > prev2:
		if b >= prev1:
			ans += 1
			prev2 = prev1
			ans += min(a, b) - prev1
	else:
		ans += min(a, b) - prev1
	prev1 = a
	prev2 = b
print(ans)


lst = [[0, 0]]
for c in range(int(input())):
	lst.append(list(map(int, input().split())))
count = 1
for r in range(len(lst) - 1):
	a = lst[r][0]
	b = lst[r][1]
	c = lst[r + 1][0]
	d = lst[r + 1][1]
	if a == b:
		count += min(c - a, d - b)
	elif a < b and c >= d or (b < a and c <= d):
		count += min(c - a, d - b) + 1
	elif a > b and c > d:
		if d >= a:
			count += d - a + 1
	elif b > a and d > c:
		if c >= b:
			count += c - b + 1
print(count)


n = int(input())
draws = 1
a1 = b1 = 0
for _ in range(n):
	(a2, b2) = [int(x) for x in input().split()]
	d = min(a2, b2) - max(a1, b1)
	draws += max(0, d)
	if a1 != b1 and d >= 0:
		draws += 1
	(a1, b1) = (a2, b2)
print(draws)


ans = int(0)
n = int(input())
na = int(0)
nb = int(0)
k = 0
for i in range(1, n + 1):
	(na, nb) = input().split()
	na = int(na)
	nb = int(nb)
	if na == nb:
		na = na + 1
	ans = ans + max(0, min(na, nb) - k + 1)
	k = max(na, nb)
print(ans)


n = int(input())
ans = 1
p1 = 0
p2 = 0
for i in range(n):
	(x, y) = map(int, input().split())
	if p1 > p2 and y >= p1:
		ans += min(x, y) - p1 + 1
	elif p2 > p1 and x >= p2:
		ans += min(x, y) - p2 + 1
	elif p1 == p2:
		ans += min(x, y) - p1
	p1 = x
	p2 = y
print(ans)


n = int(input())
(a, b, ans) = (0, 0, 1)
for _ in range(n):
	(c, d) = [int(i) for i in input().split()]
	(ok1, ok2) = (max(a, b), min(c, d))
	ans += max(0, ok2 - ok1 + int(a != b))
	(a, b) = (c, d)
print(ans)


cnt = 0
l = []
num = int(input())
tmp = num
while num:
	num -= 1
	x = input().split()
	l.append(x)
cnt += min(int(l[0][0]), int(l[0][1])) + 1
for i in range(1, tmp):
	if int(l[i][0]) == int(l[i - 1][0]) and int(l[i][1]) == int(l[i - 1][1]):
		continue
	if min(int(l[i][0]), int(l[i][1])) - max(int(l[i - 1][0]), int(l[i - 1][1])) >= 0:
		if int(l[i - 1][0]) == int(l[i - 1][1]):
			cnt += min(int(l[i][0]), int(l[i][1])) - max(int(l[i - 1][0]), int(l[i - 1][1]))
		else:
			cnt += min(int(l[i][0]), int(l[i][1])) - max(int(l[i - 1][0]), int(l[i - 1][1])) + 1
print(cnt)


n = int(input())
a = []
for i in range(n):
	(ai, bi) = map(int, input().split())
	a.append((ai, bi))
c = min(a[0]) + 1
for i in range(n - 1):
	if a[i + 1] != a[i]:
		c += min(a[i + 1]) - max(a[i]) + (1 if a[i][0] != a[i][1] else 0) if min(a[i + 1]) - max(a[i]) >= 0 else 0
print(c)


import sys
from math import ceil
read = sys.stdin.buffer.readline
mi = lambda : map(int, read().split())
li = lambda : list(mi())
cin = lambda : int(read())
ans = 1
(la, lb) = (1, 1)
for _ in range(cin()):
	(a, b) = mi()
	if (a, b) == (0, 0):
		continue
	ans += max(0, min(a, b) - max(la, lb) + 1)
	(la, lb) = (min(a, b) + 1, max(a, b))
print(ans)


from sys import stdin
max_val = int(10000000000000.0)
min_val = int(-10000000000000.0)

def read_int():
	return int(stdin.readline())

def read_ints():
	return [int(x) for x in stdin.readline().split()]

def read_str():
	return input()

def read_strs():
	return [x for x in stdin.readline().split()]
(last_x, last_y) = (0, 0)
nb_moments = int(input())
can_be = 1
for _ in range(nb_moments):
	(cur_x, cur_y) = [int(x) for x in input().split()]
	can_be += max(0, min(cur_x, cur_y) - max(last_x, last_y) + (last_x != last_y))
	(last_x, last_y) = (cur_x, cur_y)
print(can_be)


n = int(input())
x0 = 0
y0 = 0
count = 1
for i in range(n):
	(x, y) = [int(s) for s in input().split(' ')]
	if x > x0 or y > y0:
		lo = max(x0, y0)
		hi = min(x, y)
		count += max(0, hi - lo + 1)
		if x0 == y0:
			count -= 1
	(x0, y0) = (x, y)
print(count)


n = int(input())
(a, b) = ([], [])
for i in range(n):
	(x, y) = list(map(int, input().split()))
	a.append(x)
	b.append(y)
s = 0
nextd = 0
for i in range(n):
	(x, y) = (a[i], b[i])
	if i == 0:
		s += min(x, y) + 1
	else:
		xn = x - nextd
		yn = y - nextd
		if xn >= 0 and yn >= 0:
			if xn > 0 or yn > 0:
				if a[i - 1] == b[i - 1]:
					s += min(xn, yn)
				else:
					s += min(xn, yn) + 1
			elif a[i] != a[i - 1] or b[i] != b[i - 1]:
				s += 1
	nextd = max(x, y)
print(s)


n = int(input())
x = 0
c = 0
arr1 = list()
arr2 = list()
for i in range(n):
	(a, b) = map(int, input().split())
	arr1.append(a)
	arr2.append(b)
res = min(arr1[0], arr2[0]) + 1
for i in range(n - 1):
	if arr1[i] == arr1[i + 1] and arr2 == arr2[i + 1]:
		continue
	if arr1[i] < arr2[i]:
		res += max(0, min(arr1[i + 1], arr2[i + 1]) - arr2[i] + 1)
	elif arr1[i] > arr2[i]:
		res += max(0, min(arr1[i + 1], arr2[i + 1]) - arr1[i] + 1)
	else:
		res += max(0, min(arr1[i + 1], arr2[i + 1]) - arr2[i])
print(res)


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)


I = input
ans = 0
ma = 0
ap = 0
bp = 0
n = int(I())
for i in range(n):
	(a, b) = map(int, I().split())
	if a == ap and b == bp:
		if i == n - 1 and a == b:
			ans += 1
		continue
	else:
		ans += min(a, b) - ma + 1 if min(a, b) >= ma else 0
		ma = max(a, b)
		if a == b and i != n - 1:
			ans -= 1
	ap = a
	bp = b
print(ans if ans != 0 else 1)


def chPathToThis(f=None):
	import os
	f = __file__ if f is None else f
	os.chdir(os.path.dirname(f))
fle = None

def input():
	global fle

	def inner():
		if fle is None:
			from sys import stdin
			for line in stdin:
				yield line
			raise EOFError
		else:
			for line in fle:
				yield line.replace('\r', '').replace('\n', '')
			raise EOFError
	o = inner()
	return next(o)

def main():
	n = int(input())
	(cx, cy) = (0, 0)
	ans = 1
	for i in range(n):
		(a, b) = map(int, input().split())
		if b < cx or a < cy:
			pass
		else:
			if cx != cy:
				ans += 1
			(cx, cy) = (max(cx, cy), max(cx, cy))
			ans += min(a - cx, b - cy)
		(cx, cy) = (a, b)
	print(ans)
try:
	import time, numpy
	chPathToThis(__file__)
	(begin, fle) = (time.clock(), open('pytest.txt'))
	main()
	fle.close()
	print('time:', time.clock() - begin)
except ImportError:
	main()


N = int(input())
draws = 1
last_score = (0, 0)
for i in range(N):
	(x, y) = [int(x) for x in input().split()]
	current_score = (x, y)
	if current_score == last_score:
		continue
	if i == 0:
		draws += min(x, y)
	else:
		last_min = 0
		if last_score[1] < last_score[0]:
			last_min = 1
		last_max = (last_min + 1) % 2
		if current_score[last_min] - last_score[last_min] >= last_score[last_max] - last_score[last_min]:
			if last_score[0] != last_score[1]:
				draws += 1
			draws += min(current_score[0] - last_score[last_max], current_score[1] - last_score[last_max])
	last_score = (x, y)
print(draws)


import collections
n = int(input())
(a, b) = (0, 0)
ans = 1
for i in range(n):
	(c, d) = map(int, input().split())
	ans += max(min(c, d) - max(a, b) + int(a != b), 0)
	(a, b) = (c, d)
print(ans)


n = int(input())
a = 0
b = 0
ans = 1
for _ in range(n):
	(c, d) = map(int, input().split())
	m = max(a, b)
	n = min(c, d)
	ans += max(0, n - m + int(a != b))
	(a, b) = (c, d)
print(ans)


n = int(input())
c = 1
a = b = 0
for z in range(n):
	(x, y) = [int(i) for i in input().split()]
	if x == a and y == b:
		continue
	if a > b:
		if x > y:
			if y >= a:
				c += y - a + 1
		else:
			c += x - a + 1
	elif a < b:
		if x < y:
			if x >= b:
				c += x - b + 1
		else:
			c += y - b + 1
	elif a == b:
		c += min(x, y) - a
	a = x
	b = y
print(c)


n = int(input())
A = [0, 0]
result = 1
for _ in range(n):
	(x, y) = map(int, input().split())
	result += max([0, min([x, y]) - max(A) + 1])
	if A[0] == A[1]:
		result -= 1
	A = [x, y]
print(result)


n = int(input())
res = 1
(a, b) = map(int, input().split())
prev_min = min(a, b)
prev_max = max(a, b)
res += min(a, b)
for _ in range(1, n):
	(a, b) = map(int, input().split())
	cur_min = min(a, b)
	cur_max = max(a, b)
	if cur_max == prev_max and cur_min == prev_min:
		continue
	if prev_max <= cur_min:
		if prev_max != prev_min:
			res += cur_min + 1 - prev_max
		else:
			res += cur_min - prev_max
	prev_max = cur_max
	prev_min = cur_min
print(res)


from collections import defaultdict
n = int(input())
scores = [(0, 0)]
m = -1
for i in range(n):
	(h, a) = map(int, input().split())
	scores.append((h, a))
	m = max(m, max(h, a))
draws_count = 1
for i in range(1, n + 1, 1):
	x = min(scores[i])
	y = max(scores[i - 1])
	if x >= y:
		draws_count += x - y + 1
	if scores[i - 1][0] == scores[i - 1][1]:
		draws_count -= 1
print(draws_count)


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)
	if r >= l:
		c += r - l + 1
		pl = r + 1
	(pa, pb) = (a, b)
print(c)


from math import *
from sys import *
n = int(input())
cnt = 1
(a, b) = (0, 0)
last = 0
for i in range(n):
	(aa, bb) = [int(i) for i in input().split(' ')]
	(x, y) = (max(a, b, last + 1), min(aa, bb))
	cnt += max(y - x + 1, 0)
	last = y
	(a, b) = (aa, bb)
print(cnt)


debug = False
n = int(input().strip())
(ap, bp) = map(int, input().strip().split())
ans = min(ap, bp) + 1
if debug:
	print(ans)
for _ in range(n - 1):
	(a, b) = map(int, input().strip().split())
	ans1 = ans
	if min(a, b) - max(ap, bp) >= 0:
		ans += min(a, b) - max(ap, bp)
		ans += 1 if ap != bp else 0
	(ap, bp) = (a, b)
	if debug:
		print(ans - ans1)
print(ans)


def main():
	n = int(input())
	li = [(0, 0)]
	for i in range(0, n):
		(a, b) = tuple(map(int, input().split()))
		li.append((a, b))
	c = 1
	m1 = 0
	m2 = 0
	for i in range(1, n + 1):
		if m1 == m2:
			c = c + max(0, min(li[i][0], li[i][1]) - m1)
		else:
			c = c + max(0, 1 + min(li[i][0], li[i][1]) - m1)
		m1 = max(li[i][0], li[i][1])
		m2 = min(li[i][0], li[i][1])
	print(c)
main()


n = int(input())
A = [list(map(int, input().split())) for i in range(n)]
ans = 1 + min(A[0])
for i in range(1, n):
	if A[i] != A[i - 1]:
		if min(A[i]) + 1 > max(A[i - 1]):
			if A[i - 1][0] != A[i - 1][1]:
				ans += min(A[i]) - max(A[i - 1]) + 1
			else:
				ans += min(A[i]) - max(A[i - 1])
print(ans)


from math import *
from sys import *
n = int(stdin.readline())
ans = 1
(a, b) = (0, 0)
last = 0
for i in range(n):
	(aa, bb) = [int(i) for i in stdin.readline().split(' ')]
	(x, y) = (max(a, b, last + 1), min(aa, bb))
	ans += max(y - x + 1, 0)
	last = y
	(a, b) = (aa, bb)
print(ans)


n = int(input())
ans = 1
x1 = y1 = 0
ls = 0
for i in range(n):
	(x, y) = map(int, input().split())
	r = max(x1, y1)
	s = min(x, y)
	ans += max(0, s - r + int(r != ls))
	ls = s
	x1 = x
	y1 = y
print(ans)


n = int(input())
ans = 1
p = 0
q = 0
j = 0
for i in range(n):
	(l, r) = [int(x) for x in input().split()]
	if l == p and r == q:
		p = l
		q = r
		continue
	if j == 1 and r > l or (j == 2 and l > r):
		x = 1
		y = abs(min(l, r) - max(p, q))
		x += y
		ans += x
	if j == 1 and l >= r:
		if r >= p:
			ans += 1 + abs(r - p)
	if j == 2 and r >= l:
		if l >= q:
			ans += 1 + abs(q - l)
	if j == 0:
		if not (l == p or r == q):
			ans += abs(p - min(l, r))
	p = l
	q = r
	if l > r:
		j = 1
	elif l == r:
		j = 0
	else:
		j = 2
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)
	elif y >= prex:
		prey = prex
		num += 1
		num += min(x - prex, y - prey)
		(prex, prey) = (x, y)
	else:
		(prex, prey) = (x, y)
print(num)


n = int(input())
pre = [0, 0]
count = 0
peret = [-1, -1]

def func(cl1, cl2):
	return not (cl1[1] < cl2[0] or cl2[1] < cl1[0])
for i in range(n):
	s = input().split()
	(a, b) = (int(s[0]), int(s[1]))
	cl1 = [pre[0], a]
	cl2 = [pre[1], b]
	if func(cl1, cl2):
		count += abs(max(cl1[0], cl2[0]) - min(cl1[1], cl2[1])) + 1
		p = [max(cl1[0], cl2[0]), min(cl1[1], cl2[1])]
		if func(p, peret):
			count -= 1
		peret = p
	pre = [a, b]
print(count)


def count_draw(start, end):
	(a1, b1) = start
	(a2, b2) = end
	if a1 == b1 and a2 < b2:
		b1 += 1
	elif a1 == b1:
		a1 += 1
	return max(0, min(a2, b2) - max(a1, b1) + 1)
n = int(input())
last = (0, 0)
ans = 1
for _ in range(n):
	(a, b) = map(int, input().split())
	if (a, b) == last:
		continue
	ans += count_draw(last, (a, b))
	last = (a, b)
print(ans)


(x, y) = (0, 0)
ans = 1
for _ in ' ' * int(input()):
	(u, v) = map(int, input().split())
	ans += max(0, min(u, v) - max(x, y) + (x != y))
	(x, y) = (u, v)
print(ans)


def isdraw(x, y):
	return x == y

def drawb(x, y, x1, y1):
	if x == x1 and y == y1:
		return 0
	if x >= y and x1 >= y1 or (x <= y and x1 <= y1):
		(y, x) = (min(x, y), max(x, y))
		(y1, x1) = (min(x1, y1), max(x1, y1))
		if y1 >= x:
			return 1 + y1 - x - isdraw(x, y)
		else:
			return 0
	else:
		return 1 + min(x1, y1) - max(x, y) - isdraw(x, y)
ll = lambda : list(map(int, input().split()))
n = int(input())
d = 1
(x, y) = (0, 0)
for i in range(n):
	[x1, y1] = ll()
	d += drawb(x, y, x1, y1)
	(x, y) = (x1, y1)
print(d)


n = int(input())
pmax = int(-1)
pmin = int(-1)
tot = 0
for i in range(n):
	(x, y) = map(int, input().split())
	cmin = min(x, y)
	cmax = max(x, y)
	if pmin == pmax:
		pmin = pmax + 1
		pmax = pmax + 1
	if cmin - pmax >= 0:
		tot += cmin - pmax + 1
		pmin = cmin
		pmax = cmax
	else:
		pmin = cmin
		pmax = cmax
print(tot)


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)
main()


import sys
from bisect import bisect_left, bisect_right, insort
from collections import deque, Counter
from math import gcd, sqrt, factorial, ceil, log10, log2, floor
from itertools import permutations
from heapq import heappush, heappop, heapify
inf = float('inf')
mod = 1000000007

def factorial_p(n, p):
	ans = 1
	if n <= p // 2:
		for i in range(1, n + 1):
			ans = ans * i % p
	else:
		for i in range(1, p - n):
			ans = ans * i % p
		ans = pow(ans, p - 2, p)
		if n % 2 == 0:
			ans = p - ans
	return ans

def nCr_p(n, r, p):
	ans = 1
	while n != 0 or r != 0:
		(a, b) = (n % p, r % p)
		if a < b:
			return 0
		ans = ans * factorial_p(a, p) * pow(factorial_p(b, p), p - 2, p) * pow(factorial_p(a - b, p), p - 2, p) % p
		n //= p
		r //= p
	return ans

def prime_sieve(n):
	flag = n % 6 == 2
	sieve = bytearray((n // 3 + flag >> 3) + 1)
	for i in range(1, int(n ** 0.5) // 3 + 1):
		if not sieve[i >> 3] >> (i & 7) & 1:
			k = 3 * i + 1 | 1
			for j in range(k * k // 3, n // 3 + flag, 2 * k):
				sieve[j >> 3] |= 1 << (j & 7)
			for j in range(k * (k - 2 * (i & 1) + 4) // 3, n // 3 + flag, 2 * k):
				sieve[j >> 3] |= 1 << (j & 7)
	return sieve

def prime_list(n):
	res = []
	if n > 1:
		res.append(2)
	if n > 2:
		res.append(3)
	if n > 4:
		sieve = prime_sieve(n + 1)
		res.extend((3 * i + 1 | 1 for i in range(1, (n + 1) // 3 + (n % 6 == 1)) if not sieve[i >> 3] >> (i & 7) & 1))
	return res

def is_prime(n):
	if n < 5 or n & 1 == 0 or n % 3 == 0:
		return 2 <= n <= 3
	s = (n - 1 & 1 - n).bit_length() - 1
	d = n >> s
	for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
		p = pow(a, d, n)
		if p == 1 or p == n - 1 or a % n == 0:
			continue
		for _ in range(s):
			p = p * p % n
			if p == n - 1:
				break
		else:
			return False
	return True

def all_factors(n):
	(small, large) = ([], [])
	for i in range(1, int(n ** 0.5) + 1, 2 if n & 1 else 1):
		if not n % i:
			small.append(i)
			large.append(n // i)
	if small[-1] == large[-1]:
		large.pop()
	large.reverse()
	small.extend(large)
	return small

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
n = int(input())
ans = 1
(a, b) = (0, 0)
for i in range(n):
	(x, y) = get_ints()
	if min(x, y) >= max(a, b):
		ans += min(x, y) - max(a, b)
		if a != b:
			ans += 1
	(a, b) = (x, y)
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())
scores = [[0, 0]]
for _ in range(n):
	scores.append([int(item) for item in input().split()])
n += 1
ans = 1
for i in range(1, n):
	(a, b) = scores[i - 1]
	(c, d) = scores[i]
	diff = max(0, min(c, d) - max(a, b) + int(a != b))
	ans += diff if diff > 0 and (c, d) > (a, b) else 0
print(ans)


prev = [0, 0]
res = 1
for _ in range(int(input())):
	curr = [*map(int, input().split())]
	if max(prev) <= min(curr):
		res += min(curr) - (max(prev) - int(prev[0] != prev[1]))
	prev = curr
print(res)


n = int(input())
old = [0, 0]
c = 0
new = list(map(int, input().split()))
c += min(new) - max(old) + 1
old = new
for i in range(n - 1):
	new = list(map(int, input().split()))
	if old != new:
		if min(new) >= max(old):
			c += min(new) - max(old) + 1
			if old[0] == old[1]:
				c -= 1
	old = new
print(c)


n = int(input())
ans = 1
(lx, ly) = (0, 0)
for i in range(n):
	(x, y) = map(int, input().split())
	if lx < x or ly < y:
		if lx > ly:
			if lx <= y:
				ans += min(x - lx, y - lx) + 1
		elif ly <= x:
			ans += min(x - ly, y - ly) + (1 if lx != ly else 0)
	lx = x
	ly = y
print(ans)


n = int(input())
px = 0
py = 0
c = 0
for i in range(n):
	(x, y) = map(int, input().strip().split(' '))
	if i == 0:
		c += min(x, y) + 1
		px = x
		py = y
	elif x == px and y == py:
		continue
	else:
		if px == py:
			c += max(min(x, y) - max(px, py), 0)
		else:
			c += max(min(x, y) - max(px, py) + 1, 0)
		px = x
		py = y
print(c)


n = int(input())
A = []
nich = 0
kol = 0
for i in range(n):
	B = list(map(int, input().split()))
	A.append(B)
for j in range(n):
	if j == 0:
		nich += min(A[j][0], A[j][1]) + 1
		if A[j][0] == A[j][1]:
			kol += 1
	else:
		if A[j][0] == A[j][1]:
			kol += 1
		if min(A[j][0], A[j][1]) - max(A[j - 1][0], A[j - 1][1]) + 1 > 0:
			nich += min(A[j][0], A[j][1]) - max(A[j - 1][0], A[j - 1][1]) + 1
if A[n - 1][0] == A[n - 1][1]:
	print(nich - kol + 1)
else:
	print(nich - kol)


n = int(input())
res = 0
l = [(0, 0)]
for _ in range(n):
	(x, y) = list(map(int, input().split()))
	if (x, y) != l[-1]:
		l.append((x, y))
for i in range(len(l) - 1):
	(a, b) = l[i]
	(c, d) = l[i + 1]
	mn = max(a, b)
	mx = min(c, d)
	if mx >= mn:
		res += mx - mn + 1
if l == [(0, 0)]:
	res = 1
if len(l) >= 3:
	l = l[1:-1]
	for x in l:
		if x[0] == x[1]:
			res -= 1
print(res)


q = int(input())
(a, b) = (0, 0)
draws = 1
for _ in range(q):
	(c, d) = map(int, input().split())
	draws += max(0, min(c, d) - max(a, b) + (a != b))
	(a, b) = (c, d)
print(draws)


n = int(input())
cur = 'a'
last = 0
ans = 0
for i in range(n):
	(a, b) = map(int, input().split())
	if i == 0:
		ans += min(a, b) + 1
	elif cur == 'a':
		if b >= last:
			ans += min(a, b) - last + 1
	elif cur == 'b':
		if a >= last:
			ans += min(a, b) - last + 1
	elif max(a, b) > last:
		ans += min(a, b) - last
	last = max(a, b)
	if a > b:
		cur = 'a'
	elif a < b:
		cur = 'b'
	else:
		cur = 'c'
print(ans)


n = int(input())
px = 0
py = 0
ans = 0
for i in range(0, n, 1):
	(cx, cy) = map(int, input().split())
	minc = min(cx, cy)
	minp = min(px, py)
	maxp = max(px, py)
	if minc >= maxp:
		ans += minc - maxp + (minp != maxp)
	px = cx
	py = cy
print(ans + 1)


n = int(input())
k = 1
(px, py) = (0, 0)
for i in range(n):
	(x, y) = map(int, input().split())
	v = max(px, py)
	if x >= v and y >= v:
		if px != py:
			k += 1
		k += min(x - v, y - v)
	(px, py) = (x, y)
print(k)


n = int(input())
ans = 1
x_prev = [0, 0]
for i in range(n):
	x = list(map(int, input().split()))
	x.sort()
	if x == [0, 0]:
		continue
	if x_prev[0] != x_prev[1]:
		x_prev[0] = x_prev[1]
		if x_prev[1] <= x[0]:
			ans += 1
	if x[0] > x_prev[1]:
		ans += x[0] - x_prev[1]
	x_prev = x
print(ans)


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


n = int(input())
(c, d, res) = (0, 0, 1)
from sys import stdin
for i in range(n):
	(a, b) = map(int, input().split())
	(mi, ma) = (min(a, b), max(c, d))
	if a == b:
		if c == d:
			res += a - c
		else:
			res += a - ma + 1
	elif c == d:
		res += mi - c
	elif mi - ma + 1 > 0:
		res += mi - ma + 1
	(c, d) = (a, b)
print(res)


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


n = int(input())
A = 0
B = 0
count = 1
while n:
	(a, b) = map(int, input().split())
	if A == B:
		count += min(a - A, b - B)
	elif A > B:
		if b >= A:
			count += min(a - A, b - A) + 1
	elif a >= B:
		count += min(a - B, b - B) + 1
	A = a
	B = b
	n -= 1
print(count)


n = int(input())
a = 0
b = 0
c = 0
d = 0
sum = 0
while n > 0:
	n -= 1
	x = input().split(' ')
	maxx = int(max(c, d))
	minn = min(int(x[0]), int(x[1]))
	if minn >= maxx:
		sum += minn - maxx
		if c != d:
			sum += 1
	c = int(x[0])
	d = int(x[1])
print(sum + 1)


x = y = 0
r = 1
for _ in [0] * int(input()):
	(a, b) = map(int, input().split())
	r += max(0, min(a, b) - max(x, y) + (x != y))
	(x, y) = (a, b)
print(r)


def main():
	n = int(input())
	steps = []
	for i in range(n):
		steps.append([int(x) for x in input().split(' ')])
	(l, r) = (0, 0)
	ans = 1
	for steps in steps:
		(x, y) = steps
		if l == r:
			ans += min(x, y) - l
		elif l < r:
			if x >= r:
				ans += min(x, y) - r + 1
		elif y >= l:
			ans += min(x, y) - l + 1
		(l, r) = (x, y)
	print(ans)
main()


N = int(input())
data = [[0, 0]]
res = 0
max_tie = -1
for i in range(N):
	s = input()
	s = [int(x) for x in s.split(' ')]
	prev = data[-1]
	data.append([s[0], s[1]])
	inteval = [max(prev), min(s)]
	inteval[0] = max(inteval[0], max_tie)
	if inteval[1] >= inteval[0]:
		num = inteval[1] - inteval[0] + 1
		res += num
		max_tie = inteval[1] + 1
print(res)


def calc(a, b, x, y):
	if x == y:
		if x <= a and y <= b:
			return min(a - x, b - y)
	elif x < y and x < a:
		nxtX = min(y, a)
		return (nxtX == y) + calc(a, b, nxtX, y)
	elif y < x and y < b:
		nxtY = min(x, b)
		return (nxtY == x) + calc(a, b, x, nxtY)
	return 0
n = int(input().strip())
arr = [(0, 0)] * n
for i in range(n):
	arr[i] = map(int, input().strip().split(' '))
ans = 1
(x, y) = (0, 0)
for i in range(n):
	(a, b) = arr[i]
	ans += calc(a, b, x, y)
	(x, y) = (a, b)
print(ans)


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, ans) = (int(input()), 0)
(ap, bp) = (0, 0)
for i in range(n):
	(a, b) = (int(j) for j in input().split())
	mins = min(a, b)
	maxs = max(ap, bp)
	if mins >= maxs:
		ans += mins - maxs + 1
	if ap == bp:
		ans -= 1
	(ap, bp) = (a, b)
print(ans + 1)


import sys
import math as mt
import bisect
input = sys.stdin.readline
t = 1
p = 10 ** 9 + 7

def ncr_util():
	inv[0] = inv[1] = 1
	fact[0] = fact[1] = 1
	for i in range(2, 300001):
		inv[i] = inv[i % p] * (p - p // i) % p
	for i in range(1, 300001):
		inv[i] = inv[i - 1] * inv[i] % p
		fact[i] = fact[i - 1] * i % p

def solve():
	j = 1
	ans = 1
	for i in range(len(l)):
		ans += max(0, min(l[i][0], l[i][1]) - j + 1)
		if l[i][0] == l[i][1]:
			j = l[i][1] + 1
		j = max(j, max(l[i][0], l[i][1]))
	return ans
for _ in range(t):
	n = int(input())
	l = []
	for i in range(n):
		l.append(list(map(int, input().split())))
	print(solve())


import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict, deque, Counter, OrderedDict
import threading

def main():
	D = [[*map(int, input().split())] for _ in range(int(input()))]
	visit = defaultdict(lambda : 0)
	ans = 0
	(last_x, last_y) = (0, -1)
	for i in range(len(D)):
		(x, y) = D[i]
		if min(x, y) >= max(last_x, last_y) and (not visit[str(x) + '-' + str(y)]):
			z = min(x, y) - max(last_x, last_y) + 1
			if last_x == last_y:
				z -= 1
			ans += z
		visit[str(x) + '-' + str(y)] = 1
		(last_x, last_y) = (x, y)
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


d = 0
ans = 0
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	ans += max(0, min(x, y) - d + 1)
	d = max(d, max(x, y) + (x == y))
print(ans)


t = int(input())
scores = []
scores.append([0, 0])
for i in range(t):
	scores.append(list(map(int, input().split())))
count = 0
for i in range(1, t + 1):
	if scores[i][1] >= scores[i - 1][0] and scores[i][0] >= scores[i - 1][1]:
		temp = min(scores[i][0], scores[i][1]) - max(scores[i - 1][0], scores[i - 1][1])
		if scores[i - 1][0] - scores[i - 1][1] != 0:
			temp += 1
		count += temp
	else:
		continue
print(count + 1)


t = int(input())
nb = 0
(a, b) = map(int, input().split())
al = []
al.append([a, b])
nb = min(a, b) + 1
for i in range(t - 1):
	(a1, b1) = map(int, input().split())
	if a1 == a and b1 == b:
		continue
	else:
		al.append([a1, b1])
		a = a1
		b = b1
for i in range(len(al) - 1):
	v = min(al[i + 1][0], al[i + 1][1]) - max(al[i][0], al[i][1]) + 1
	if al[i][0] == al[i][1]:
		v = v - 1
	if v > 0:
		nb = nb + v
print(nb)


n = input()
n = int(n)
a = input()
a = a.split()
a = [int(i) for i in a]
answer = min(a) + 1
(x, y) = (a[0], a[1])
for i in range(n - 1):
	a = input()
	a = a.split()
	a = [int(_) for _ in a]
	_max = max(x, y)
	_min = min(a)
	answer = answer + max(0, _min - _max + 1)
	if x == y:
		answer = answer - 1
	(x, y) = (a[0], a[1])
print(answer)


n = int(input())
s = 0
(px, py) = (0, 0)
endpoints = set()
for _ in range(n):
	(nx, ny) = map(int, input().split())
	p = max(px, py)
	n = min(nx, ny)
	if n >= p:
		s += max(n - p - 1, 0)
		for x in (p, n):
			if x not in endpoints:
				s += 1
				endpoints.add(x)
	(px, py) = (nx, ny)
print(s)


n = int(input())
arr = []
for _ in range(n):
	(a, b) = map(int, input().split())
	arr.append([a, b])
ans = min(arr[0]) + 1
for i in range(n - 1):
	if max(arr[i]) <= min(arr[i + 1]):
		if arr[i][0] == arr[i][1]:
			ans -= 1
		ans += min(arr[i + 1]) - max(arr[i]) + 1
print(ans)


n = input()
n = int(n)
i1 = 0
i2 = 0
ans = 0
for i in range(0, n):
	str = input()
	(a, b) = str.split()
	a = int(a)
	b = int(b)
	mn = min(a, b)
	mx = max(i1, i2)
	if mn >= mx:
		ans = ans + mn - mx + 1
		i1 = max(mn + 1, a)
		i2 = max(mn + 1, b)
	else:
		i1 = max(i1, a)
		i2 = max(i2, b)
print(ans)


n = int(input())
scores = [tuple(map(int, input().split())) for _ in range(n)]
(x, y) = (0, 0)
draws = 1
for (x1, y1) in scores:
	if (x, y) == (x1, y1):
		continue
	(l, m) = (max(x, y), min(x1, y1))
	if m - l + 1 > 0:
		draws += m - l + 1
		if x == y:
			draws -= 1
	(x, y) = (x1, y1)
print(draws)


n = int(input())
(pre_a, pre_b, a, b) = (0, 0, 0, 0)
ans = 1
for i in range(n):
	(a, b) = map(int, input().split())
	if pre_a == pre_b:
		ans += max(0, min(a, b) - pre_a)
	else:
		ans += max(0, min(a, b) - max(pre_a, pre_b) + 1)
	(pre_a, pre_b) = (a, b)
print(ans)


import sys
input = sys.stdin.readline
from collections import defaultdict as dc
from collections import Counter
from bisect import bisect_right, bisect_left
import math
from operator import itemgetter
from heapq import heapify, heappop, heappush
n = int(input())
(x, y) = (0, 0)
c = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	if a != x or b != y:
		if x <= b and y <= a:
			if y >= x and b <= a:
				if x == y:
					c += abs(b - y)
				elif x > y:
					c += abs(b - y)
				else:
					c += abs(b - y) + 1
			elif x >= y and a <= b:
				if x == y:
					c += abs(a - x)
				elif y > x:
					c += abs(a - x)
				else:
					c += abs(a - x) + 1
			elif y >= x and b >= a:
				if x == y:
					c += abs(a - y)
				elif x > y:
					c += abs(a - y)
				else:
					c += abs(a - y) + 1
			elif x >= y and a >= b:
				if x == y:
					c += abs(b - x)
				elif y > x:
					c += abs(b - x)
				else:
					c += abs(b - x) + 1
	(x, y) = (a, b)
c += 1
print(c)