You invited $n$ guests to dinner! You plan to arrange one or more circles of chairs. Each chair is going to be either occupied by one guest, or be empty. You can make any number of circles. 

Your guests happen to be a little bit shy, so the $i$-th guest wants to have a least $l_i$ free chairs to the left of his chair, and at least $r_i$ free chairs to the right. The "left" and "right" directions are chosen assuming all guests are going to be seated towards the center of the circle. Note that when a guest is the only one in his circle, the $l_i$ chairs to his left and $r_i$ chairs to his right may overlap.

What is smallest total number of chairs you have to use?


-----Input-----

First line contains one integer $n$  — number of guests, ($1 \leqslant n \leqslant 10^5$). 

Next $n$ lines contain $n$ pairs of space-separated integers $l_i$ and $r_i$ ($0 \leqslant l_i, r_i \leqslant 10^9$).


-----Output-----

Output a single integer — the smallest number of chairs you have to use.


-----Examples-----
Input
3
1 1
1 1
1 1

Output
6

Input
4
1 2
2 1
3 5
5 3

Output
15

Input
1
5 6

Output
7



-----Note-----

In the second sample the only optimal answer is to use two circles: a circle with $5$ chairs accomodating guests $1$ and $2$, and another one with $10$ chairs accomodationg guests $3$ and $4$.

In the third sample, you have only one circle with one person. The guest should have at least five free chairs to his left, and at least six free chairs to his right to the next person, which is in this case the guest herself. So, overall number of chairs should be at least 6+1=7.

Difficulty: HARD
Tags: Mathematics

Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
n = int(input())
leftSpaces = []
rightSpaces = []
for i in range(n):
	(left, right) = list(map(int, input().split(' ')))
	leftSpaces += [left]
	rightSpaces += [right]
leftSpaces.sort()
rightSpaces.sort()
chairs = 0
for i in range(n):
	chairs += 1
	chairs += max(leftSpaces[i], rightSpaces[i])
print(chairs)


import heapq
n = int(input())
res = 0
left = []
right = []
for _ in range(n):
	(l, r) = list(map(int, input().split()))
	left.append(l)
	right.append(r)
heapq.heapify(left)
heapq.heapify(right)
for _ in range(n):
	(l, r) = (heapq.heappop(left), heapq.heappop(right))
	res += max(l, r)
print(res + n)


n = int(input())
a = []
m = []
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	m.append(y)
a.sort()
m.sort()
ans = 0
for i in range(n):
	ans += max(a[i], m[i]) + 1
print(ans)


n = int(input())
res = 0
left = []
right = []
for _ in range(n):
	(l, r) = list(map(int, input().split()))
	left.append(l)
	right.append(r)
left.sort()
right.sort()
for _ in range(n):
	res += max(left[_], right[_])
print(res + n)


qtd = int(input())
esquerdas = []
direitas = []
totalCadeiras1Circulo = 0
totalCadaUmCirculo = 0
for gfdsga in range(qtd):
	(l, r) = list(map(int, input().split()))
	esquerdas.append(l)
	direitas.append(r)
esquerdasOrdenadas = sorted(esquerdas)
direitasOrdenadas = sorted(direitas)
for x in range(qtd):
	totalCadaUmCirculo += 1 + max(esquerdas[x], direitas[x])
	totalCadeiras1Circulo += 1 + max(esquerdasOrdenadas[x], direitasOrdenadas[x])
print(min(totalCadeiras1Circulo, totalCadaUmCirculo))


n = int(input())
r = []
l = []
for i in range(n):
	(a, b) = map(int, input().split())
	r.append(a)
	l.append(b)
s = n
L1 = sorted(r)
L2 = sorted(l)
for i in range(n):
	s = s + max(L1[i], L2[i])
print(s)


import sys
left = []
right = []
n = int(input())
for _ in range(n):
	lr = input().split()
	left.append(int(lr[0]))
	right.append(int(lr[1]))
sun = 0
left.sort()
right.sort()
for i in range(n):
	sun += max(left[i], right[i])
print(sun + n)


n = int(input())
a = []
b = []
for i in range(n):
	(l, r) = [int(_) for _ in input().strip().split()]
	a.append(l)
	b.append(r)
a.sort()
b.sort()
print(n + sum([max(a[_], b[_]) for _ in range(n)]))


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
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')

class SegmentTree:

	def __init__(self, data, default=-10 ** 6, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree1:

	def __init__(self, data, default=10 ** 6, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
MOD = 10 ** 9 + 7

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod = 10 ** 9 + 7
omod = 998244353
prime = [True for i in range(200001)]
pp = [0] * 200001

def SieveOfEratosthenes(n=200000):
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
n = int(input())
ans = n
l = []
r = []
for i in range(n):
	(x, y) = map(int, input().split())
	l.append(x)
	r.append(y)
l.sort()
r.sort()
for i in range(n):
	ans += max(l[i], r[i])
print(ans)


n = int(input())
(l, r) = ([], [])
for i in range(n):
	(a, b) = map(int, input().split())
	l.append(a)
	r.append(b)
l.sort(reverse=True)
r.sort(reverse=True)
count = 0
for i in range(n):
	count += max(l[i], r[i]) + 1
print(count)


n = int(input())
(l, r) = ([], [])
for i in range(n):
	(li, ri) = map(int, input().split())
	l.append(li)
	r.append(ri)
l.sort()
r.sort()
print(n + sum([max(l[i], r[i]) for i in range(n)]))


x = int(input())
lst1 = []
lst2 = []
for i in range(x):
	(a, b) = map(int, input().split())
	lst1.append(a)
	lst2.append(b)
lst1 = sorted(lst1)
lst2 = sorted(lst2)
sum = 0
for i in range(len(lst1)):
	sum += max(lst1[i], lst2[i])
print(sum + x)


n = input()
n = int(n)
l = []
r = []
for i in range(n):
	s = input().split()
	s = list(map(int, s))
	l.append(s[0])
	r.append(s[1])
l = sorted(l)
r = sorted(r)
res = 0
for i in range(n):
	res += max(l[i], r[i]) + 1
print(res)


left = []
right = []
n = int(input())
for _ in range(0, n):
	user = list(map(int, input().split()))
	left.append(user[0])
	right.append(user[1])
left.sort(reverse=True)
right.sort(reverse=True)
ans = n
for i in range(0, len(left)):
	ans += max(left[i], right[i])
print(ans)


print(sum([max(*x) + 1 for x in zip(*list(map(sorted, list(zip(*[list(map(int, input().split())) for _ in range(int(input()))])))))]))


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()]
nb_guest = read_int()
(left, rite) = ([], [])
for _ in range(nb_guest):
	(a, b) = read_ints()
	left.append(a)
	rite.append(b)
left = sorted(left)
rite = sorted(rite)
answ = nb_guest
for i in range(nb_guest):
	answ += max(left[i], rite[i])
print(answ)


n = int(input())
alist = []
blist = []
res = 0
for i in range(n):
	(a, b) = map(int, input().split())
	alist.append(a)
	blist.append(b)
alist.sort()
blist.sort()
for i in range(n):
	res += max(alist[i], blist[i]) + 1
print(res)


def mainA():
	n = int(input())
	s = input()
	cnt = 0
	for i in s:
		if i == '8':
			cnt += 1
	print(min(cnt, n // 11))

def mainB():

	def get(n):
		ret = 0
		while n > 0:
			ret += n % 10
			n //= 10
		return ret
	n = int(input())
	if n <= 9:
		print(n)
		return
	t = 9
	while n > t:
		t = t * 10 + 9
	t //= 10
	print(get(t) + get(n - t))

def mainD():
	(n, A, B, ans) = (int(input()), [], [], 0)
	for i in range(n):
		(a, b) = list(map(int, input().split()))
		A.append(a)
		B.append(b)
	A.sort()
	B.sort()
	for i in range(n):
		ans += max(A[i], B[i])
	print(ans + n)
mainD()


n = int(input())
l = []
r = []
for i in range(n):
	lst = input().split(' ')
	lst = list(map(int, lst))
	l.append(lst[0])
	r.append(lst[1])
l.sort()
r.sort()
sum = 0
for i in range(n):
	sum += max(l[i], r[i])
print(sum + n)


n = int(input())
a = []
b = []
for i in range(n):
	(u, v) = map(int, input().split())
	a.append(u)
	b.append(v)
a.sort()
b.sort()
val = n
for i in range(n):
	val += max(a[i], b[i])
print(val)


n = int(input())
l = []
r = []
for _ in range(n):
	x = [int(a) for a in input().split()]
	l.append(x[0])
	r.append(x[1])
l.sort()
r.sort()
print(n + sum([max(l[i], r[i]) for i in range(n)]))


n = int(input())
l = []
r = []
for i in range(n):
	(t1, t2) = input().split(' ')
	l.append(int(t1))
	r.append(int(t2))
l.sort()
r.sort()
ans = n
for i in range(n):
	ans += max(l[i], r[i])
print(ans)


n = int(input())
ls = []
rs = []
for _ in range(n):
	(l, r) = [int(a) for a in input().split()]
	ls.append(l)
	rs.append(r)
ls.sort()
rs.sort()
print(sum((1 + max(a, b) for (a, b) in zip(ls, rs))))


n = int(input())
left = []
right = []
for i in range(n):
	(a, b) = [int(x) for x in input().split(' ')]
	left.append(a)
	right.append(b)
left.sort()
right.sort()
sum = 0
for i in range(n):
	sum += max(left[i], right[i])
print(sum + n)


n = int(input())
spaceLst = []
for i in range(n):
	lr = [int(x) for x in input().split()]
	spaceLst.append(lr)
leftLst = []
rightLst = []
for i in range(n):
	leftLst.append(spaceLst[i][0])
	rightLst.append(spaceLst[i][1])
leftLst.sort()
rightLst.sort()
res = 0
for i in range(n):
	res += max(leftLst[i], rightLst[i]) + 1
print(res)


n = int(input())
x = []
r = 0
for i in range(n):
	(a, b) = map(int, input().split())
	x.append((a, b))
x1 = sorted(x, key=lambda y: y[0], reverse=True)
x2 = sorted(x, key=lambda y: y[1], reverse=True)
for i in range(n):
	r += max(x1[i][0], x2[i][1]) + 1
print(r)


n = int(input())
(ps, qs) = ([], [])
for _ in range(n):
	(p, q) = list(map(int, input().split()))
	ps.append(p)
	qs.append(q)
ps.sort(reverse=True)
qs.sort(reverse=True)
res = n
for i in range(n):
	res += max(ps[i], qs[i])
print(res)


n = int(input())
(l, r) = ([0] * n, [0] * n)
for i in range(n):
	(l[i], r[i]) = map(int, input().split())
print(sum((max(a, b) for (a, b) in zip(sorted(l), sorted(r)))) + n)


r = n = int(input())
ar = [list(map(int, input().split())) for _ in range(n)]
a1 = sorted(ar, key=lambda x: x[0])
a2 = sorted(ar, key=lambda x: x[1])
for i in range(n):
	r += max(a1[i][0], a2[i][1])
print(r)


from collections import defaultdict
import io, os, math
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
rr = lambda : input()
rri = lambda : int(input())
rrm = lambda : list(map(int, input().split()))
INF = float('inf')

def solve(N, pairs):
	chairs = N
	left = [v[0] for v in pairs]
	right = [v[1] for v in pairs]
	left.sort()
	right.sort()
	for i in range(N):
		chairs += max(left[i], right[i])
	return chairs
N = rri()
pairs = []
for _ in range(N):
	pairs.append(rrm())
print(solve(N, pairs))


guests = int(input())
right_chairs_wanted = [0] * guests
left_chairs_wanted = [0] * guests
guest_controller = 0
while guest_controller < guests:
	(left, right) = input().split()
	left_chairs_wanted[guest_controller] = int(left)
	right_chairs_wanted[guest_controller] = int(right)
	guest_controller += 1
right_chairs_wanted = sorted(right_chairs_wanted)
left_chairs_wanted = sorted(left_chairs_wanted)
total_chairs = guests
for idx in range(guests):
	total_chairs += max(right_chairs_wanted[idx], left_chairs_wanted[idx])
print(total_chairs)


n = int(input())
left = []
right = []
for i in range(n):
	(l, r) = input().split()
	left.append(int(l))
	right.append(int(r))
left = sorted(left)
right = sorted(right)
res = n
for i in range(n):
	res += max(left[i], right[i])
print(res)


n = int(input())
a = []
b = []
for i in range(n):
	inp = [int(x) for x in input().split(' ')]
	a.append(inp[0])
	b.append(inp[1])
a.sort()
b.sort()
ans = 0
for i in range(n):
	ans = ans + max(a[i], b[i]) + 1
print(ans)


n = int(input())
l = [0 for i in range(n)]
r = [0 for i in range(n)]
for i in range(n):
	[l[i], r[i]] = map(int, input().split())
l.sort()
r.sort()
res = n
for i in range(n):
	res += max(l[i], r[i])
print(res)


a = [[], []]
_ = ([[a[i].append(int(x)) for (i, x) in enumerate(input().split())] for _ in range(int(input()))], [x.sort() for x in a], print(len(a[0]) + sum((max(x, y) for (x, y) in zip(*a)))))


def get_input_list():
	return list(map(int, input().split()))
n = int(input())
l = []
r = []
for _ in range(n):
	(li, ri) = get_input_list()
	l.append(li)
	r.append(ri)
l.sort()
r.sort()
res = n
for i in range(n):
	res += max(l[i], r[i])
print(res)


n = int(input())
l = []
r = []
for i in range(n):
	(x, y) = map(int, input().split())
	l.append(x)
	r.append(y)
l.sort()
r.sort()
print(n + sum(map(max, l, r)))


n = int(input())
a = []
b = []
for _ in range(n):
	(x, y) = list(map(int, input().split()))
	a.append(x)
	b.append(y)
a.sort()
b.sort()
print(n + sum(map(max, a, b)))


import itertools
l1 = []
l2 = []
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	l1.append(x)
	l2.append(y)
l1.sort()
l2.sort()
sum1 = 0
for (a, b) in zip(l1, l2):
	sum1 += max(a, b)
print(sum1 + len(l1))


import math
l1 = int(input())
l = []
r = []
for i in range(l1):
	l2 = input().split()
	l2 = [int(i) for i in l2]
	l.append(l2[0])
	r.append(l2[1])
l.sort()
r.sort()
resp = l1
for i in range(l1):
	resp += max(l[i], r[i])
print(resp)


import sys
lines = sys.stdin.readlines()
n = int(lines[0])
l = list()
r = list()
lines = lines[1:]
for i in range(n):
	words = lines[i].split(' ')
	l.append(int(words[0]))
	r.append(int(words[1]))
l.sort()
r.sort()
ans = 0
for i in range(n):
	ans += max(l[i], r[i])
ans += n
print(ans)


import sys

def rint():
	return map(int, sys.stdin.readline().split())
n = int(input())
r = [0] * n
l = [0] * n
for i in range(n):
	(r[i], l[i]) = rint()
r.sort()
l.sort()
ans = 0
for i in range(n):
	ans += max(r[i], l[i])
print(ans + n)


n = int(input())
arr_l = []
arr_r = []
for i in range(0, n):
	entrada = input().split()
	arr_l.append(int(entrada[0]))
	arr_r.append(int(entrada[1]))
result = n
arr_l.sort()
arr_r.sort()
i = 0
while i < n:
	result += max(arr_l[i], arr_r[i])
	i += 1
print(result)


n = int(input())
l = []
r = []
for i in range(n):
	numbers_in_line = [int(num) for num in input().split()]
	(l_new, r_new) = numbers_in_line
	l.append(l_new)
	r.append(r_new)
l.sort()
r.sort()
maxes = [max(lv, rv) for (lv, rv) in zip(l, r)]
print(n + sum(maxes))


n = int(input())
ans = 0
valueA = [0] * 100000
valueB = [0] * 100000
for i in range(n):
	values = input()
	values = values.split()
	valueA[i] = int(values[0])
	valueB[i] = int(values[1])
valueA = sorted(valueA, reverse=True)
valueB = sorted(valueB, reverse=True)
i = n - 1
while i >= 0:
	ans += max(valueA[i], valueB[i])
	i -= 1
print(ans + n)


import sys
import bisect
try:
	fin = open('in')
except:
	fin = sys.stdin
input = fin.readline
n = int(input())
l = []
r = []
for _ in range(n):
	(x, y) = map(int, input().split())
	l.append(x)
	r.append(y)
l.sort()
r.sort()
ans = n
for i in range(n):
	ans += max(l[i], r[i])
print(ans)


n = int(input())
left = []
right = []
for i in range(n):
	(l, r) = map(int, input().split())
	left.append(l)
	right.append(r)
left.sort()
right.sort()
m = 0
for i in range(n):
	m += max(left[i], right[i])
print(m + n)


n = int(input())
left = []
right = []
for i in range(n):
	(l, r) = map(int, input().split())
	left.append(l)
	right.append(r)
left = sorted(left)
right = sorted(right)
total = n
for i in range(n):
	total += max(left[i], right[i])
print(total)


from collections import defaultdict, deque
from heapq import heappush, heappop
from math import inf
ri = lambda : map(int, input().split())

def solve():
	n = int(input())
	left = []
	right = []
	for _ in range(n):
		(l, r) = ri()
		left.append(l)
		right.append(r)
	left.sort()
	right.sort()
	ans = 0
	for i in range(n):
		ans += max(left[i], right[i])
	ans += n
	print(ans)
t = 1
while t:
	t -= 1
	solve()


n = int(input())
left = []
right = []
for i in range(n):
	(a, b) = map(int, input().split())
	left.append(a)
	right.append(b)
print(n + sum(map(max, sorted(left), sorted(right))))


n = eval(input())
lstl = []
lstr = []
for x in range(n):
	lst = input().split()
	lstl.append(eval(lst[0]))
	lstr.append(eval(lst[1]))
lstl.sort()
lstr.sort()
count = n
for i in range(n):
	count += max(lstl[i], lstr[i])
print(count)


r = []
l = []
saida = 0
n = int(input())
tam = 0
for i in range(n):
	entrada = input().split(' ')
	l.append(int(entrada[0]))
	r.append(int(entrada[1]))
l.sort()
r.sort()
for j in range(n):
	saida = saida + max(l[j], r[j]) + 1
print(saida)


n = int(input())
l = []
r = []
for i in range(n):
	(a, b) = map(int, input().split())
	l.append(a)
	r.append(b)
r = sorted(r)
l = sorted(l)
ss = n
for i in range(n):
	ss += max(l[i], r[i])
print(ss)


import sys
input = sys.stdin.readline
n = int(input())
lr = [list(map(int, input().split())) for i in range(n)]
L = [lr[i][0] for i in range(n)]
R = [lr[i][1] for i in range(n)]
L.sort()
R.sort()
ANS = 0
for i in range(n):
	ANS += max(L[i], R[i])
print(ANS + n)


n = int(input())
a = []
b = []
for _ in range(n):
	(a1, b1) = list(map(int, input().split()))
	a.append(a1)
	b.append(b1)
a.sort()
b.sort()
soma = 0
for i in range(n):
	soma += max(a[i], b[i]) + 1
print(soma)


n = int(input())
a = []
b = []
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
res = n
a.sort()
b.sort()
for i in range(n):
	res += max(a[i], b[i])
print(res)


n = int(input())
ans = n
(l, r) = ([], [])
for i in range(n):
	(n, m) = map(int, input().split())
	l.append(n)
	r.append(m)
l.sort()
r.sort()
ans += sum(map(max, l, r))
print(ans)


from sys import stdin
n = int(stdin.readline())
left = []
right = []
for x in range(n):
	(l, r) = [int(x) for x in stdin.readline().split()]
	left.append(l)
	right.append(r)
left.sort()
right.sort()
total = n
for x in range(n):
	total += max(left[x], right[x])
print(total)


n = int(input())
e = []
d = []
for x in range(n):
	arr = [int(x) for x in input().split()]
	e.append(arr[0])
	d.append(arr[1])
e.sort()
d.sort()
resp = 0
for i in range(n):
	resp += max(e[i], d[i]) + 1
print(resp)


n = int(input())
nums1 = []
nums2 = []
for i in range(n):
	(a, b) = map(int, input().split())
	nums1.append(a)
	nums2.append(b)
nums1 = sorted(nums1)
nums2 = sorted(nums2)
res = n
for i in range(n):
	res += max(nums1[i], nums2[i])
print(res)


import heapq
n = int(input())
fa = [i for i in range(n)]
ls = []
rs = []
for i in range(n):
	(l, r) = [int(x) for x in input().split()]
	ls.append((l, i))
	rs.append((r, i))
ls.sort()
rs.sort()
ans = n
for i in range(n):
	ans += max(ls[i][0], rs[i][0])
print(ans)


n = int(input())
l = []
r = []
for _ in range(n):
	(x, y) = map(int, input().split())
	l.append(x)
	r.append(y)
print(n + sum(map(max, sorted(l), sorted(r))))


guests = int(input())
chairs = 0
right = []
left = []
for i in range(guests):
	(r, l) = (int(x) for x in input().split())
	right.append(r)
	left.append(l)
right = sorted(right)
left = sorted(left)
for i in range(guests):
	chairs += max(right[i], left[i])
print(chairs + guests)