Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special — it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to s and elevator initially starts on floor s at time 0.

The elevator takes exactly 1 second to move down exactly 1 floor and negligible time to pick up passengers. Genos is given a list detailing when and on which floor passengers arrive. Please determine how long in seconds it will take Genos to bring all passengers to floor 0.


-----Input-----

The first line of input contains two integers n and s (1 ≤ n ≤ 100, 1 ≤ s ≤ 1000) — the number of passengers and the number of the top floor respectively.

The next n lines each contain two space-separated integers f_{i} and t_{i} (1 ≤ f_{i} ≤ s, 1 ≤ t_{i} ≤ 1000) — the floor and the time of arrival in seconds for the passenger number i.


-----Output-----

Print a single integer — the minimum amount of time in seconds needed to bring all the passengers to floor 0.


-----Examples-----
Input
3 7
2 1
3 8
5 2

Output
11

Input
5 10
2 77
3 33
8 21
9 12
10 64

Output
79



-----Note-----

In the first sample, it takes at least 11 seconds to bring all passengers to floor 0. Here is how this could be done:

1. Move to floor 5: takes 2 seconds.

2. Pick up passenger 3.

3. Move to floor 3: takes 2 seconds.

4. Wait for passenger 2 to arrive: takes 4 seconds.

5. Pick up passenger 2.

6. Go to floor 2: takes 1 second.

7. Pick up passenger 1.

8. Go to floor 0: takes 2 seconds.

This gives a total of 2 + 2 + 4 + 1 + 2 = 11 seconds.

Difficulty: EASY
Tags: Mathematics

Implementation
Expected Time Complexity: O(NLOG(N))

Starter code:

Solutions:
def read():
	inputs = input().strip()
	return list(map(int, inputs.split()))

def read_pair():
	return map(int, input().split(' '))
(n, s) = read_pair()
mx = s
for i in range(n):
	(x, y) = read_pair()
	mx = max(mx, x + y)
print(mx)


(N, S) = map(int, input().split())
arrivals = [0 for i in range(S + 1)]
for i in range(N):
	(f, t) = map(int, input().split())
	arrivals[f] = max(arrivals[f], t)
for i in range(S, 0, -1):
	arrivals[i - 1] = max(arrivals[i - 1], arrivals[i] + 1)
print(arrivals[0])


from queue import *
(n, s) = map(int, input().split())
ja = [False] * 1001
fila = PriorityQueue()
e = 0
for i in range(n):
	(f, t) = map(int, input().split())
	fila.put([-f, -t])
while not fila.empty():
	(f, t) = fila.get()
	f *= -1
	t *= -1
	if not ja[f]:
		ja[f] = True
		e += s - f
		s = f
		if t > e:
			e += t - e
e += s
print(e)


import sys
from collections import defaultdict, deque, Counter
from math import factorial
from fractions import gcd
from itertools import product, combinations
sys.setrecursionlimit(1000000)
data = sys.stdin.read().splitlines()
(N, S) = map(int, data[0].split())
arr = [0] * (S + 1)
for line in data[1:]:
	(f, t) = map(int, line.split())
	arr[f] = max(arr[f], t)
ans = 0
for i in range(S, -1, -1):
	ans = max(ans, arr[i])
	ans += 1
print(ans - 1)


(n, s) = map(int, input().split())
time = s
for i in range(n):
	(f, t) = map(int, input().split())
	time = max(time, f + t)
print(time)


(n, s) = map(int, input().split())
t = 0
floor = []
pt = []
for i in range(0, n):
	(a, b) = map(int, input().split())
	floor.append(a)
	pt.append(b)
t = s - floor[n - 1]
for i in range(n - 1, -1, -1):
	if i != n - 1:
		t += floor[i + 1] - floor[i]
	if pt[i] <= t:
		continue
	elif pt[i] > t:
		t = pt[i]
t += floor[0]
print(t)


(n, s) = list(map(int, input().split()))
(cf, ct) = (s, 0)
for (f, t) in sorted((list(map(int, input().split())) for i in range(n)), reverse=True):
	ct = max(ct + cf - f, t)
	cf = f
print(ct + cf)


(n, m) = map(int, input().split())
ans = m
for x in range(n):
	(a, b) = map(int, input().split())
	ans = max(ans, a + b)
print(ans)


(n, s) = map(int, input().split())
level = [0] * (s + 1)
for i in range(n):
	(f, t) = map(int, input().split())
	level[f] = max(level[f], t)
t = 0
for i in range(s, 0, -1):
	t = max(t, level[i]) + 1
print(t)


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


from math import sqrt

def inp():
	return map(int, input().split())
(n, s) = inp()
cf = s
li = []
for i in range(n):
	(f, at) = inp()
	li.append([f, at])
ct = -1
for j in range(s, -1, -1):
	ct += 1
	cf = j
	m = 0
	for i in li:
		if i[0] == cf and i[1] > m:
			m = i[1]
	ct += max(0, m - ct)
print(ct)


def cunt():
	(n, s) = map(int, input().split())
	ans = s
	for i in range(n):
		(f, t) = map(int, input().split())
		ans = max(ans, t + f)
	print(ans)
cunt()


(n, s) = map(int, input().split())
mylist = []
for i in range(n):
	(f, t) = map(int, input().split())
	mylist.append([f, t])
mylist.append([0, 0])
mylist.sort(reverse=True)
time = 0
i = 0
while s > 0:
	if s == mylist[i][0]:
		if time < mylist[i][1]:
			time = mylist[i][1]
		i += 1
	if s > mylist[i][0]:
		s -= 1
		time += 1
print(time)


import math
(n, s) = [int(x) for x in input().split()]
pas = []
for i in range(n):
	(f, t) = [int(x) for x in input().split()]
	pas.append((t, f))
pas.sort(key=lambda pair: pair[0] + pair[1])
print(max(pas[-1][0] + pas[-1][1], s))


(N, top) = list(map(int, input().split()))
pa = dict()
for i in range(N):
	(f, t) = list(map(int, input().split()))
	pa[f] = max(pa.get(f, 0), t)
(now, time) = (top, 0)
for (f, t) in sorted(list(pa.items()), reverse=True):
	time += now - f
	now = f
	time = max(t, time)
else:
	time += now
print(time)


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


(group_num, start) = [int(i) for i in input().split()]
arrivals = {0: 0}
for _ in range(group_num):
	(floor, time) = [int(i) for i in input().split()]
	if floor not in arrivals:
		arrivals[floor] = time
	arrivals[floor] = max(arrivals[floor], time)
floors = sorted(arrivals.keys(), reverse=True)
last_floor = start
time = 0
for f in floors:
	time = max(time + abs(f - last_floor), arrivals[f])
	last_floor = f
print(time)


import sys
(n, s) = tuple([int(i) for i in input().split(' ')])
temp = {}
for i in range(n):
	(key, value) = [int(j) for j in input().split(' ')]
	if key in temp and value > temp[key] or key not in temp:
		temp[key] = value
time = 0
while s > 0:
	if s in temp and temp[s] > time:
		time = temp[s]
	time = time + 1
	s = s - 1
print(time)


(n, s) = map(int, input().split())
MAXN = 1010
T = [-1] * MAXN
for i in range(n):
	(f, t) = map(int, input().split())
	T[f] = max(T[f], t)
now = 0
nowf = s
while nowf > 0:
	now = max(now, T[nowf])
	nowf -= 1
	now += 1
print(now)


import sys
from math import sqrt, log, log2, ceil, log10, gcd, floor, pow, sin, cos, tan, pi, inf, factorial
from copy import copy, deepcopy
from sys import exit, stdin, stdout
from collections import Counter, defaultdict, deque
from itertools import permutations
import heapq
from bisect import bisect_left
from bisect import bisect_right
mod = 1000000007
iinp = lambda : int(sys.stdin.readline())
inp = lambda : sys.stdin.readline().strip()
strl = lambda : list(inp().strip().split(' '))
intl = lambda : list(map(int, inp().split(' ')))
mint = lambda : map(int, inp().split())
flol = lambda : list(map(float, inp().split(' ')))
flush = lambda : stdout.flush()

def solve():
	(n, s) = mint()
	cm = 0
	for i in range(n):
		(fi, ti) = mint()
		if i == 0:
			cm = fi + ti
		if i != 0:
			if fi + ti > cm:
				cm = fi + ti
	if cm > s:
		print(cm)
	else:
		print(s)
t = 1
for _ in range(t):
	solve()


(n, s) = map(int, input().split())
floor = []
for i in range(n):
	(a, b) = map(int, input().split())
	floor.append([a, b])
floor.sort(key=lambda x: x[0], reverse=True)
t = 0
index = 0
floornow = s
while index < n:
	t += floornow - floor[index][0]
	if t < floor[index][1]:
		t = floor[index][1]
	floornow = floor[index][0]
	index += 1
t += floornow
print(t)


def solve():
	(N, S) = map(int, input().split())
	PS = []
	for i in range(N):
		(f, t) = map(int, input().split())
		PS.append((f, t))
	PS.sort(key=lambda p: -p[0])
	ans = 0
	for (f, t) in PS:
		ans += S - f
		S = f
		if ans < t:
			ans = t
	if S != 0:
		ans += S
	print(ans)
solve()


import sys
lines = sys.stdin.read().splitlines()
(N, S) = [int(x) for x in lines[0].strip().split(' ')]
floors = []
time = 0
cur = S

def time_elapsed(cur, floor, passnum, t):
	temp = abs(cur - floor)
	if passnum - t < temp:
		return temp
	return passnum - t
for line in lines[1:]:
	(F, T) = [int(x) for x in line.split(' ')]
	floors.append((F, T))
while len(floors) > 0:
	s_fl = sorted(floors, key=lambda x: x[0], reverse=True)
	s_pas = sorted(floors, key=lambda x: x[1])
	t1 = time_elapsed(cur, s_fl[0][0], s_fl[0][1], time)
	t2 = time_elapsed(cur, s_pas[0][0], s_pas[0][1], time)
	cur = s_fl[0][0]
	floors.remove(s_fl[0])
	time = time + t1
print(time + cur)


from sys import stdin
(n, s) = map(int, input().split())
floor = []
for i in range(n):
	(f, t) = map(int, input().split())
	floor.append((f, t))
arr = sorted(floor, reverse=True)
back = s
cnt = 0
for i in range(n):
	p = arr[i]
	num = p[0]
	t = p[1]
	cnt += back - num
	if cnt < t:
		cnt += t - cnt
	back = num
	if i == n - 1 and num > 0:
		cnt += num
print(cnt)


(n, s) = tuple(map(int, input().split()))
maxes = []
for i in range(n):
	(a, b) = tuple(map(int, input().split()))
	maxes.append(b - s + a)
print(max(maxes) + s if max(maxes) > 0 else s)


import sys
import math
import collections
from pprint import pprint
mod = 1000000007

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, s) = map(int, input().split())
a = []
for i in range(n):
	temp = list(map(int, input().split()))
	a.append(temp)
a.sort(reverse=True)
t = 0
for x in a:
	t += s - x[0]
	s = x[0]
	t += max(0, x[1] - t)
if s != 0:
	t += s
print(t)


import math
(n, s) = tuple(map(int, input().split()))
floors = [0 for i in range(s + 1)]
for i in range(n):
	(floor, time) = tuple(map(int, input().split()))
	floors[floor] = max(floors[floor], time)
res = s
for i in range(s + 1):
	res = max(res, floors[i] + i)
print(res)


(n, s) = map(int, input().split())
f = []
maxi = s
c = 0
for index in range(n):
	(x, y) = map(int, input().split())
	f.append([x, y])
	if maxi < x + y:
		maxi = x + y
print(maxi)


n = list(map(int, input().split()))
count = 0
m = []
for i in range(n[0]):
	x = list(map(int, input().split()))
	m.append(x)
f = len(m) - i - 1
for i in range(n[0]):
	f = len(m) - i - 1
	if i == 0:
		count += n[1] - m[f][0]
	if i != 0:
		count += m[f + 1][0] - m[f][0]
	if count < m[f][1]:
		count += m[f][1] - count
count += m[0][0]
print(count)


n = list(map(int, input().split()))
lst = []
for i in range(n[0]):
	n1 = list(map(int, input().split()))
	lst.append(n1)
lst.sort()
lst.reverse()
w = n[1] - lst[0][0]
for j in range(len(lst)):
	if j == 0:
		if w < lst[j][1]:
			w = w + (lst[j][1] - w)
	else:
		w = w + (lst[j - 1][0] - lst[j][0])
		if w < lst[j][1]:
			w = w + (lst[j][1] - w)
m = lst[-1][0] - 0
print(w + m)


def main():
	(n, s) = map(int, input().split(' '))
	m = {}
	for i in range(n):
		(f, t) = map(int, input().split(' '))
		if f not in m:
			m[f] = t
		else:
			m[f] = max(t, m[f])
	time = 0
	while s > 0:
		if s in m:
			time += max(0, m[s] - time)
		s -= 1
		time += 1
	print(time)
main()


(n, s) = map(int, input().split())
A = []
T = []
for i in range(n):
	(f, t) = map(int, input().split())
	A.append(f)
	T.append(t)
for i in range(n):
	for j in range(i + 1, n):
		if A[i] < A[j]:
			t1 = A[j]
			t2 = T[j]
			A[j] = A[i]
			A[i] = t1
			T[j] = T[i]
			T[i] = t2
time = 0
if n > 1:
	for i in range(n):
		if i > 0:
			time += A[i - 1] - A[i]
			s = A[i]
		else:
			time += s - A[i]
		if time < T[i]:
			time += T[i] - time
else:
	time += s - A[0]
	if time < T[0]:
		time += T[0] - time
	s = A[0]
if s > 0:
	time += s
print(time)


IL = lambda : list(map(int, input().split()))
IS = lambda : input().split()
I = lambda : int(input())
S = lambda : input()
(n, s) = IL()
print(max([s] + [sum(IL()) for i in range(n)]))


(n, m) = map(int, input().split())
s = [[int(i) for i in input().split()] for j in range(n)]
s = s[::-1] + [[0] * 2]
time = m - s[0][0]
level = s[0][0]
for i in range(n):
	if time < s[i][1]:
		time += s[i][1] - time
	time += level - s[i + 1][0]
	level = s[i + 1][0]
print(time)


(n, m) = map(int, input().split())
t = m
for i in range(n):
	(a, b) = map(int, input().split())
	t = max(t, a + b)
print(t)


(n, s) = map(int, input().split())
(f, t) = map(int, input().split())
p = max(s, f + t)
for i in range(n - 1):
	(f, t) = map(int, input().split())
	v = max(s, f + t)
	if p < v:
		p = v
print(p)


ns = input().split()
for i in range(2):
	ns[i] = int(ns[i])
ft = [input().split() for i in range(ns[0])]
for i in range(ns[0]):
	for x in range(2):
		ft[i][x] = int(ft[i][x])
passengers = [[] for i in range(ns[1])]
for i in range(ns[0]):
	passengers[ft[i][0] - 1].append(ft[i][1])
for i in range(ns[1]):
	if passengers[i] == []:
		passengers[i] = 0
	else:
		passengers[i] = max(passengers[i])
time = 0
floor = ns[1]
while floor > 0:
	if passengers[floor - 1] == 0:
		time += 1
		floor -= 1
	elif time >= passengers[floor - 1]:
		passengers[floor - 1] = 0
	else:
		time += passengers[floor - 1] - time
print(time)


(n, s) = map(int, input().split())
passengers = dict()
for i in range(n):
	(floor, time) = map(int, input().split())
	if floor not in passengers.keys():
		passengers[floor] = time
		continue
	if time > passengers[floor]:
		passengers[floor] = time
key = sorted(passengers.keys())[::-1]
seconds = 0
current_floor = s
counter = 0
while key:
	floor = key[counter]
	del key[counter]
	time = passengers[floor]
	seconds += current_floor - floor
	current_floor = floor
	if seconds < time:
		seconds += time - seconds
while current_floor != 0:
	current_floor -= 1
	seconds += 1
print(seconds)


(n, s) = map(int, input().split())
mass = []
for i in range(n):
	(floor, t) = map(int, input().split())
	mass.append((floor, t))
floor = s
mass.sort(key=lambda x: x[0], reverse=True)
if s - mass[0][0] > mass[0][1]:
	time = s - mass[0][0]
else:
	time = mass[0][1]
floor = mass[0][0]
mass.append((0, 0))
for i in range(1, n + 1):
	if floor - mass[i][0] > mass[i][1] - time:
		time += floor - mass[i][0]
	else:
		time = mass[i][1]
	floor = mass[i][0]
print(time)


(n, s) = map(int, input().split())
hotel = [0 for _ in range(s + 1)]
for _ in range(n):
	(f, t) = map(int, input().split())
	hotel[f] = max(hotel[f], t)
time = -1
for floor in range(s, -1, -1):
	time += 1
	if hotel[floor] > time:
		time = hotel[floor]
print(time)


(n, s) = map(int, input().split())
x = []
for i in range(n):
	(a, b) = map(int, input().split())
	x.append([a, b])
x.sort(key=lambda i: i[0], reverse=True)
c = 0
p = s
for i in x:
	c = max(i[1], c + p - i[0])
	p = i[0]
c += i[0]
print(c)


import sys
from math import *

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

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

def mints():
	return map(int, minp().split())
(n, s) = mints()
a = [0] * (s + 1)
for i in range(n):
	(f, t) = mints()
	a[f] = max(a[f], t)
for i in range(s - 1, -1, -1):
	a[i] = max(a[i + 1] + 1, a[i])
print(a[0])


inf = list(map(int, input().split()))
(n, s) = (inf[0], inf[1])

class Node:

	def __init__(self, f, t):
		self.f = f
		self.t = t

def get(p):
	return p.f
arr = [None] * n
for i in range(n):
	inf = list(map(int, input().split()))
	arr[i] = Node(inf[0], inf[1])
arr = sorted(arr, key=get, reverse=True)
time = 0
last = s
for node in arr:
	floor_traversed = last - node.f
	time += floor_traversed
	if node.t > time:
		time = time + node.t - time
	last = node.f
print(time + arr[-1].f)


(n, s) = map(int, input().split())
floors = [0] * s
for i in range(n):
	(floor, time) = map(int, input().split())
	floors[floor - 1] = max(floors[floor - 1], time)
curr_time = floors[-1]
for i in range(s - 2, -1, -1):
	curr_time = max(curr_time + 1, floors[i])
print(curr_time + 1)


(n, k) = list(map(int, input().split()))
t = []
for j in range(n):
	s = list(map(int, input().split()))
	t.append(s)
t.sort()
f = t[::-1]
p = 0
h = k
for i in range(n):
	a = h - f[i][0]
	if f[i][1] > a + p:
		a += f[i][1] - (a + p)
	p += a
	h = f[i][0]
print(p + h)


(n, s) = map(int, input().split())
mx = s
for i in range(n):
	(a, b) = map(int, input().split())
	mx = max(mx, a + b)
print(mx)


(n, s) = map(int, input().split())
d = []
for i in range(n):
	(a, b) = map(int, input().split())
	d.append((a, b))
d.sort(reverse=True)
ans = 0
curr = s
i = 0
while i < n:
	ans += curr - d[i][0]
	curr = d[i][0]
	if d[i][1] > ans:
		ans = d[i][1]
	i += 1
print(ans + d[n - 1][0])


(n, s) = map(int, input().split())
f = [0] * (s + 1)
for i in range(n):
	(a, b) = map(int, input().split())
	if b > f[a]:
		f[a] = b
i = s
k = 0
while i != 0:
	if f[i] > k:
		k = f[i]
	i -= 1
	k += 1
print(k)


(n, s) = map(int, input().split())
a = []
for i in range(n):
	(f, t) = map(int, input().split())
	a.append([f, t])
a.sort(reverse=True)
ans = 0
f = int(s)
for i in a:
	ans += f - i[0]
	if i[1] > ans:
		ans = i[1]
	f = i[0]
print(f + ans)


(n, s) = map(int, input().split())
ans = 0
st = s
a = [[0] * 2] * n
for i in range(n):
	a[i] = list(map(int, input().split()))
a.sort(reverse=True)
for i in range(n):
	ans += st - a[i][0]
	st = a[i][0]
	if a[i][1] > ans:
		ans += a[i][1] - ans
ans += st
print(ans)


(n, s) = map(int, input().split(' '))
answer = s
arr = []
for _ in range(n):
	arr.append(tuple(map(int, input().split(' '))))
arr.sort(reverse=True)
for _ in range(n):
	(f, t) = arr[_]
	g = t - s + f
	if g > 0:
		s = s + g
		answer += g
print(answer)


(n, m) = map(int, input().split())
fl = []
ps = []
fl.append(0)
ps.append(0)
for i in range(0, n):
	(a, b) = map(int, input().split())
	fl.append(a)
	ps.append(b)
lv = m
time = 0
for i in range(n, -1, -1):
	time += lv - fl[i]
	if ps[i] > time:
		time += ps[i] - time
	lv = fl[i]
print(time)


I = lambda : map(int, input().split())
(n, s) = I()
exec('a=sorted([' + n * '[*I()],' + '])[::-1]')
r = t = 0
for (x, y) in a:
	v = t + s - x
	if v < y:
		v = y
	r += v - t
	t = v
	s = x
print(r + s)


(n, s) = str(input()).split()
n = int(n)
s = int(s)
refer = dict()
for i in range(n):
	line = str(input()).split()
	line = [int(it) for it in line]
	if line[0] in refer.keys():
		line[1] = max(line[1], refer.get(line[0]))
	refer.update({line[0]: line[1]})
lines = list()
for (k, v) in refer.items():
	lines.append([k, v])
lines.sort(key=lambda x: x[0], reverse=True)
lines.append([0, 0])
num = 0
for line in lines:
	num += max(s - line[0], line[1] - num)
	s = line[0]
print(num)


(n, s) = map(int, input().split())
arr = []
for i in range(n):
	arr.append([])
	arr[i] = [int(i) for i in input().split()]
arr = sorted(arr, reverse=True, key=lambda x: x[0])
(ans, c) = (0, 0)
for i in range(n):
	if i != 0:
		c = arr[i - 1][0]
	if i == 0:
		ans = ans + s - arr[i][0]
	else:
		ans = ans + c - arr[i][0]
	if arr[i][1] >= ans:
		ans = ans + (arr[i][1] - ans)
ans = ans + arr[n - 1][0]
print(ans)


(n, p) = map(int, input().split())
li = [0] * 1100
time = 0
while n:
	(a, b) = map(int, input().split())
	li[a] = max(li[a], b)
	n -= 1
for i in range(1050, -1, -1):
	if li[i]:
		time += p - i
		p = i
		if li[i] > time:
			time += li[i] - time
print(time + p)


(n, s) = map(int, input().split())
fi = []
ti = []
fx = s
total = 0
for i in range(n):
	(f, t) = map(int, input().split())
	fi.append(f)
	ti.append(t)
for i in range(n - 1, -1, -1):
	ttrvl = fx - fi[i]
	fx = fi[i]
	total += ttrvl
	if total < ti[i]:
		total = total + (ti[i] - total)
total = total + fi[0]
print(total)


info = [int(x) for x in input().split(' ')]
n = info[0]
least = info[1]
for i in range(n):
	t = sum([int(x) for x in input().split(' ')])
	if t > least:
		least = t
print(least)


(n, s) = map(int, input().split())
maxi = 0
for i in range(n):
	(a, b) = map(int, input().split())
	maxi = max(maxi, max(b - (s - a), 0))
print(maxi + s)


(n, s) = map(int, input().split())
fs = []
ts = []
for i in range(n):
	(f, t) = map(int, input().split())
	fs += [f]
	ts += [t]
(ts, fs) = (list(x) for x in zip(*sorted(zip(ts, fs), key=lambda pair: pair[1], reverse=True)))
r = 0
r = s - fs[0]
i = 0
while i < len(fs) - 1:
	if ts[i] > r:
		r += ts[i] - r
	r += fs[i] - fs[i + 1]
	i += 1
if ts[-1] > r:
	r += ts[-1] - r
print(r + fs[-1])


__author__ = 'Utena'
import operator
(n, s) = map(int, input().split())
p = [list(map(int, input().split())) for i in range(n)]
p = sorted(p, key=operator.itemgetter(0, 1), reverse=0)
t = []
for j in range(n):
	t.append(p[j][0] + p[j][1])
print(max(max(t), s))


(n, s) = map(int, input().split())
l = sorted((list(map(int, input().split())) for _ in range(n)))[::-1]
t = s - l[0][0]
pre = 0
for i in range(n):
	t += l[pre][0] - l[i][0]
	pre = i
	if t < l[i][1]:
		t = l[i][1]
print(t + l[-1][0])


(n, m) = map(int, input().split())
a = []
for _ in range(n):
	(x, y) = map(int, input().split())
	a.append((x, y))
a.sort(reverse=True)
ans = 0
prev = m
for i in a:
	ans += prev - i[0]
	prev = i[0]
	if i[1] > ans:
		ans += i[1] - ans
print(ans + prev)


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


(i, floor) = input().split()
floors = []
for j in range(int(i)):
	floors.append(list(map(int, input().split())))
current = int(floor)
f = len(floors) - 1
time = 0
while f >= 0:
	time += current - floors[f][0]
	current = floors[f][0]
	if floors[f][1] > time:
		time += floors[f][1] - time
	f -= 1
print(time + current)


(n, s) = map(int, input().split())
p = [0] * n
for i in range(0, n):
	(f, t) = map(int, input().split())
	p[i] = f + t
print(max(max(p), s))


line1 = [int(x) for x in input().split()]
ppl = line1[0]
fl = line1[1]
pretable = []
table = []
for i in range(0, ppl):
	pretable.append([int(x) for x in input().split()])
for i in range(0, len(pretable)):
	low = 9999
	lowIndex = -1
	for j in range(0, len(pretable)):
		if pretable[j] not in table and pretable[j][0] < low:
			lowIndex = j
			low = pretable[j][0]
	table.append(pretable[lowIndex])
i = len(table) - 1
t = 0
curFloor = fl
while i >= 0:
	t += curFloor - table[i][0]
	curFloor = table[i][0]
	if table[i][1] > t:
		t = table[i][1]
	i -= 1
t += curFloor
print(t)


import sys
(passage, nbFloor) = list(map(int, input().split()))
maxi = nbFloor
for i in range(passage):
	(floor, attente) = list(map(int, sys.stdin.readline().split()))
	maxi = max(maxi, floor + attente)
print(maxi)


def f(ll, s):
	tl = [l[0] + l[1] for l in ll]
	tl.append(s)
	return max(tl)
(n, s) = list(map(int, input().split()))
ll = [list(map(int, input().split())) for _ in range(n)]
print(f(ll, s))


(n, ans) = map(int, input().split())
for i in range(n):
	(f, t) = map(int, input().split())
	ans = max(ans, f + t)
print(ans)


from collections import defaultdict
(n, s) = list(map(int, input().split()))
dic = defaultdict(lambda : 0)
time = 0
for _ in range(n):
	(fi, ti) = list(map(int, input().split()))
	dic[fi] = max(ti, dic[fi])
i = 0
for (key, value) in dic.items():
	time += s - key
	if time < value:
		time = value
	s = key
	i += 1
	if i == len(dic):
		time += s
print(time)


def hotel(a, n):
	time = 0
	time = time + n - a[len(a) - 1][0]
	for i in reversed(range(len(a))):
		if i != len(a) - 1:
			time = time + a[i + 1][0] - a[i][0]
		if time < a[i][1]:
			time = time + (a[i][1] - time)
	time = time + a[0][0]
	print(time)
t = list(map(int, input('').split()))
a = []
for i in range(t[0]):
	b = list(map(int, input('').split()))
	a.append(b)
hotel(a, t[1])


(n, s) = map(int, input('').split())
lst = []
time = 0
for i in range(n):
	lst.append(list(map(int, input().split())))
lst.sort(reverse=True)
for item in lst:
	time += s - item[0]
	s = item[0]
	if time >= item[1]:
		pass
	else:
		time += item[1] - time
print(time + s)


(n, s) = map(int, input().split())
l = []
for _ in range(n):
	l.append(list(map(int, input().split())))
l.sort(reverse=True)
t = 0
for i in range(n):
	t += s - l[i][0]
	s = l[i][0]
	if l[i][1] > t:
		t = l[i][1]
t += s
print(t)


class Event:

	def __init__(self, floor, time):
		(self.floor, self.time) = (floor, time)
(n, floor) = map(int, input().split())
events = [Event(*map(int, input().split())) for i in range(n)]
events.sort(key=lambda event: (-event.floor, event.time))
time = 0
for event in events:
	time = max(event.time, time + floor - event.floor)
	floor = event.floor
time += floor
print(time)


import collections
import random
import sys
try:
	from tqdm import tqdm
except:

	def tqdm(iterable):
		return iterable
__taskname = ''
if __taskname:
	sys.stdin = open(__taskname + '.in')
	sys.stdout = open(__taskname + '.out', 'w')
(n, s) = list(map(int, input().split()))
time = 0
for (f, t) in sorted((list(map(int, input().split())) for x in range(n))):
	time = max(time + s - f, t)
	s = f
print(time + s)


(n, s) = [int(x) for x in input().split()]
ans = s
for i in range(n):
	(f, t) = [int(x) for x in input().split()]
	ans = max(ans, t + f)
print(ans)


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

def arr_2d(n):
	return [[int(x) for x in input().split()] for i in range(n)]
(n, s) = inp()
arr = arr_2d(n)
count = 0
for i in range(n - 1, -1, -1):
	count += s - arr[i][0]
	s = arr[i][0]
	if arr[i][1] - count > 0:
		count += arr[i][1] - count
print(count + arr[0][0])


(n, s) = [int(x) for x in input().split()]
max_value = s
for i in range(n):
	value = sum([int(x) for x in input().split()])
	if value > max_value:
		max_value = value
print(max_value)


(n, s) = list(map(int, input().split(' ')[:2]))
res = 0
for i in range(n):
	(f, t) = list(map(int, input().split(' ')[:2]))
	time = max(t, s - f) + f
	res = max(res, time)
print(res)


import sys
import math
from functools import reduce
import bisect

def getN():
	return int(input())

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

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

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

def index(a, x):
	i = bisect.bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	return -1
(n, s) = getNM()
pasengers = []
for i in range(n):
	pasengers.append(list(getNM()))
pasengers.sort(reverse=True)
ans = s
t = 0
for (a, b) in pasengers:
	t += s - a
	ans += b - t if b - t > 0 else 0
	t += b - t if b - t > 0 else 0
	s = a
print(ans)


(n, s) = list(map(int, input().split()))
p = []
for i in range(n):
	p.append(list(map(int, input().split())))
p.sort(reverse=True)
time = 0
floor = s
index = 0
while floor > 0 and index < len(p):
	if floor < p[index][0]:
		index += 1
	elif floor == p[index][0]:
		if time >= p[index][1]:
			time += 1
			floor -= 1
			index += 1
		else:
			time += 1
	else:
		time += 1
		floor -= 1
print(time + floor)


(n, s) = [int(x) for x in input().split()]
pos = s * [0]
for i in range(n):
	(a, b) = [int(x) for x in input().split()]
	pos[a - 1] = max(pos[a - 1], b)
pos.reverse()
t = 0
for i in range(s):
	if pos[i] > t:
		t = pos[i]
	t += 1
print(t)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, operator
from sys import stdin, stdout
(passg, floors) = [int(x) for x in stdin.readline().rstrip().split()]
c = int(passg)
datas = list()
while c > 0:
	(f, t) = (0, 0)
	(f, t) = [int(x) for x in stdin.readline().rstrip().split()]
	datas.append([f, t])
	c = c - 1
datas = sorted(datas, key=operator.itemgetter(0), reverse=True)
tims = 0
tims = floors - datas[0][0]
for (indx, val) in enumerate(datas):
	if datas[indx][1] > tims:
		tims = tims + (datas[indx][1] - tims)
	if indx + 1 >= len(datas):
		tims = tims + (datas[indx][0] - 0)
	else:
		tims = tims + (datas[indx][0] - datas[indx + 1][0])
stdout.write(str(tims) + '\n')


def aval(cur, t):
	if cur >= t:
		return True
	return False
(pas, flo) = map(int, input().split())
cur = 0
people = []
for i in range(pas):
	(f, t) = map(int, input().split())
	people.append((f, t))
people.sort(reverse=True)
aim = flo
for i in range(len(people)):
	nex = people[i]
	cur += aim - nex[0]
	if not aval(cur, nex[1]):
		cur += nex[1] - cur
	aim = nex[0]
cur += aim
print(cur)


(n, s) = map(int, input().split())
flats = {}
for i in range(n):
	(f, t) = map(int, input().split())
	if f not in flats:
		flats[f] = 0
	flats[f] = max(flats[f], t)
curr_time = 0
for i in range(s, -1, -1):
	if i in flats:
		if curr_time < flats[i]:
			curr_time = flats[i]
	curr_time += 1
print(curr_time - 1)


intro = input()
variables = [int(x) for x in intro.split(' ')]
passengers = variables[0]
currentFloor = variables[1]
passengerFloorLines = []
for stop in range(passengers):
	passengerFloorLines.append(input())
floorList = []
floorTimes = {}
for line in passengerFloorLines:
	variables = line.split(' ')
	floor = int(variables[0])
	time = int(variables[1])
	floorList.append(floor)
	if floor in floorTimes:
		floorTimes[floor] = max(floorTimes[floor], time)
	else:
		floorTimes[floor] = time
floorList = sorted(floorList)[::-1]
currentTime = 0
for floor in floorList:
	currentTime += currentFloor - floor
	currentFloor = floor
	if floorTimes[floor] > currentTime:
		currentTime += floorTimes[floor] - currentTime
if currentFloor != 0:
	currentTime += currentFloor
print(currentTime)


(n, m) = [int(x) for x in input().split()]
pas = []
time = 0
for i in range(n):
	pas.append([int(x) for x in input().split()])
for temp in pas[::-1]:
	time += m - temp[0]
	m -= m - temp[0]
	if time < temp[1]:
		time = temp[1]
print(time + m)


(n, t) = map(int, input().split())
m_t = 0
for _ in range(n):
	(f, t_f) = map(int, input().split())
	m_t = max(m_t, t + max(0, t_f - t + f))
print(m_t)


import sys
(n, s) = map(int, input().split())
time = 0
a = []
for i in range(n):
	a.append(tuple(map(int, input().split())))
a.sort()
a = list(reversed(a))
time = 0
for i in range(n):
	time += s - a[i][0]
	s = a[i][0]
	time = max(a[i][1], time)
time += a[-1][0]
print(time)


ch = input()
d = ch.split()
n = int(d[0])
s = int(d[1])
dic = {}
F = []
TT = []
for i in range(n):
	ch = input()
	d = ch.split()
	f = int(d[0])
	F.append(f)
	t = int(d[1])
	TT.append(t)
m = 0
T = s - F[len(F) - 1]
for i in range(n - 1, -1, -1):
	T += m
	if TT[i] - T > 0:
		T += TT[i] - T
	if i > 0:
		m = F[i] - F[i - 1]
	else:
		m = F[i]
print(T + m)


(n, s) = map(int, input().split())
ans = s
for i in range(n):
	(x, y) = map(int, input().split())
	ans = max(ans, x + y)
print(ans)


(n, up) = map(int, input().split())
res = 0
for i in range(n):
	(fl, t) = map(int, input().split())
	res = max(res, max(t, up - fl) + fl)
print(res)


a = input().split(' ')
(n, s) = (int(a[0]), int(a[1]))
d = {}
while n > 0:
	n -= 1
	a = input().split(' ')
	(f, t) = (int(a[0]), int(a[1]))
	d.update({f: max(d.get(f, 1), t)})
k = d.get(s, 0)
for i in range(s)[::-1]:
	k += max(d.get(i, 1) - k, 1)
print(k)


from sys import stdin, stdout, stderr, setrecursionlimit
setrecursionlimit(100000)

def debug(*e):
	if not __debug__:
		print(*e, file=stderr)

def dd(*vals):
	import inspect, re
	frame = inspect.getframeinfo(inspect.stack()[1][0])
	vs = re.search('dd\\((.+)\\)', frame.code_context[0]).group(1).split(',')
	if vs:
		debug(','.join(('{0} = {1}'.format(vs[i], v) for (i, v) in enumerate(vals))))

def trace(f):

	def traced(*args, **kw):
		debug('calling {} with args {}, {}'.format(f.__name__, args, kw))
		return f(*args, **kw)
	return traced

def read():
	return stdin.readline().rstrip()

def readarr(sep=None, maxsplit=-1):
	return read().split(sep, maxsplit)

def readint():
	return int(read())

def readintarr(sep=None, maxsplit=-1):
	return [int(a) for a in readarr(sep, maxsplit)]

def write(*args, **kwargs):
	sep = kwargs.get('sep', ' ')
	end = kwargs.get('end', '\n')
	stdout.write(sep.join((str(a) for a in args)) + end)

def writearr(arr, sep=' ', end='\n'):
	stdout.write(sep.join((str(a) for a in arr)) + end)
(n, s) = readintarr()
d = [0] * (s + 1)
for _ in range(n):
	(f, t) = readintarr()
	d[f] = max(d[f], t)
t = 0
for i in range(s, 0, -1):
	dt = d[i] - t
	t += 1 + max(0, dt)
write(t)


def hotel(lst, z):
	m = z
	for elem in lst:
		m = max(m, elem[0] + elem[1])
	return m
(n, s) = [int(i) for i in input().split()]
a = list()
for j in range(n):
	(f, t) = [int(z) for z in input().split()]
	a.append([f, t])
print(hotel(a, s))


(n, s) = map(int, input().split())
mx = 0
for i in range(0, n):
	(x, y) = map(int, input().split())
	mx = max(mx, x + y)
print(max(mx, s))


(n, s) = map(int, input().split())
passengers = dict()
for i in range(n):
	(floor, time) = map(int, input().split())
	if floor in passengers.keys():
		if time > passengers[floor]:
			passengers[floor] = time
	else:
		passengers[floor] = time
seconds = 0
current_floor = s
while passengers.keys():
	floor = max(passengers.keys())
	time = passengers[floor]
	seconds += current_floor - floor
	current_floor = floor
	del passengers[floor]
	if seconds < time:
		seconds += time - seconds
while current_floor != 0:
	current_floor -= 1
	seconds += 1
print(seconds)


(n, k) = map(int, input().split())
for i in range(n):
	(f, t) = map(int, input().split())
	if k - f < t:
		k = f + t
print(k)


X = list(map(int, input().split()))
Passengers = []
for i in range(X[0]):
	Passengers.append(list(map(int, input().split())))
Time = max(Passengers[-1][1], X[1] - Passengers[-1][0])
Prev = Passengers[-1][0]
for i in range(X[0] - 1, -1, -1):
	Time = max(Time + Prev - Passengers[i][0], Passengers[i][1])
	Prev = Passengers[i][0]
print(Time + Prev)


k = []
(a, b) = list(map(int, input().split(' ')))
for i in range(a):
	(x, y) = list(map(int, input().split(' ')))
	k.append([x, y])
k.append([0, -1])
k.sort()
k.reverse()
curr = b
t = 0
for i in k:
	d = curr - i[0]
	curr = i[0]
	t = max(i[1], t + d)
print(t)


I = lambda : map(int, input().split())
(n, s) = I()
print(max((max(sum(I()), s) for _ in '0' * n)))


import sys
if False:
	inp = open('A.txt', 'r')
else:
	inp = sys.stdin
(n, s) = map(int, inp.readline().split())
ans = s
for i in range(n):
	(floor, time) = map(int, inp.readline().split())
	if s - floor < time:
		ans = max(ans, time + floor)
	else:
		pass
print(ans)


(N, S) = map(int, input().split())
stuff = [map(int, input().split()) for i in range(N)]
answer = S
for (a, k) in stuff:
	answer = max(answer, a + k)
print(answer)


import sys
from collections import deque
from typing import Union
input = sys.stdin.readline

def print(x, end='\n'):
	sys.stdout.write(str(x) + end)

def main():
	T = 1

	def solve():
		arr = list(map(int, input().split(' ')))
		(n, s) = (arr[0], arr[1])
		res = [0] * n
		for x in range(n):
			arr = list(map(int, input().split(' ')))
			(f, t) = (arr[0], arr[1])
			res[x] = (f, t)
		res.sort(key=lambda x: x[0])
		res.reverse()
		Time = []
		curr_elem = res[0]
		max_time = 0
		for x in range(n):
			if res[x][0] == curr_elem[0]:
				max_time = max(max_time, res[x][1])
			else:
				Time.append((curr_elem[0], max_time))
				curr_elem = res[x]
				max_time = curr_elem[1]
		Time.append(res[-1])
		elapsed = 0
		floor = s
		for x in Time:
			elapsed += floor - x[0]
			floor = x[0]
			elapsed += max(0, x[1] - elapsed)
		elapsed += Time[-1][0]
		print(elapsed)
	for f in range(T):
		solve()
	pass
main()


def main():
	(n, s) = [int(t) for t in input().split()]
	passengers = [tuple((int(t) for t in input().split())) for _ in range(n)]
	sorted(passengers, key=lambda p: -p[0])
	lack = 0
	for (f, t) in passengers:
		current_time = s - f + lack
		lack += max(t - current_time, 0)
	print(s + lack)
main()


(passngers, startFloor) = map(int, input().split())
totalTime = 0
l = []
for i in range(passngers):
	supl = list(map(int, input().split()))
	l.append(supl)
l = sorted(l, key=lambda x: x[0], reverse=True)
for i in range(len(l)):
	totalTime += startFloor - l[i][0]
	startFloor = l[i][0]
	if l[i][1] - totalTime > 0:
		totalTime += l[i][1] - totalTime
	else:
		continue
if l[-1][0] > 0:
	totalTime += l[-1][0]
print(totalTime)


(n, m) = list(map(int, input().split()))
(max_i, max_v) = (0, 0)
answ = 0
for i in range(n):
	(ind, value) = list(map(int, input().split()))
	if answ < value + ind:
		answ = value + ind
if m < answ:
	print(answ)
	exit(0)
print(m)


(n, s) = map(int, input().split())
(a, c) = ([0] * 1010, s)
for _ in range(n):
	(y, x) = map(int, input().split())
	c = max(c, y + x)
print(c)


(n, s) = map(int, input().split())
sm = 0
p = []
for i in range(n):
	(x, y) = map(int, input().split())
	p.append([x, y])
p = sorted(p, reverse=True)
f = s
t = 0
for i in p:
	t = t + (f - i[0])
	if i[1] > t:
		t = t + (i[1] - t)
	f = i[0]
t = t + p[n - 1][0]
print(t)


(a, b) = (list(map(int, input().split())), [])
for i in range(a[0]):
	(c, d) = (list(map(int, input().split())), [i[0] for i in b])
	if c[0] not in d:
		b.append(c)
	elif c[1] > b[d.index(c[0])][1]:
		b.pop(d.index(c[0]))
		b.append(c)
(b, time) = (dict(b), 0)
for i in range(a[1], 0, -1):
	try:
		if time < b[i]:
			time += b[i] - time
	except KeyError:
		pass
	time += 1
print(time)


(n, s) = list(map(int, input().split()))
L = [list(map(int, input().split())) for _ in range(n)]
T = [s - k for k in range(s + 1)]
for A in L:
	T[A[0]] = max([T[A[0]], A[1]])
r = 0
for k in range(s, 0, -1):
	r = max([r, T[k]])
	r += 1
print(r)


(n, s) = map(int, input().split())
l = [input().split() for i in [0] * n]
m = 0
for i in l:
	m = [m, int(i[0]) + int(i[1])][int(i[0]) + int(i[1]) > m]
print(max(m, s))


(n, s) = list(map(int, input().split()))
lp = list()
for i in range(n):
	lp.append(tuple(map(int, input().split())))
lp.sort(reverse=True)
time = 0
current_floor = s
for i in range(n):
	predicted_time = time + current_floor - lp[i][0]
	passenger_atime = lp[i][1]
	if passenger_atime > predicted_time:
		time = passenger_atime
	else:
		time = predicted_time
	current_floor = lp[i][0]
time += current_floor
print(time)


a = input().split()
n = int(a[0])
s = int(a[1])
alls = {}
for i in range(n):
	p = input().split()
	floor = int(p[0])
	time = int(p[1])
	if floor in alls:
		alls[floor].append(time)
	else:
		alls[floor] = [time]
time = 0
for f in range(s, 0, -1):
	if f in alls:
		if max(alls[f]) > time:
			time = max(alls[f])
	time += 1
print(time)


(n, s) = map(int, input().split())
curtime = 0
a = []
for _ in range(n):
	a.append(tuple(map(int, input().split())))
a.sort()
a.reverse()
for i in a:
	curtime += max(s - i[0], i[1] - curtime)
	s = i[0]
print(curtime + a[-1][0])


(n, k) = map(int, input().split())
res = k
for i in range(n):
	(x, y) = map(int, input().split())
	res = max(res, x + y)
print(res)


(a, b) = map(int, input().split())
ans = b
for i in range(a):
	(c, d) = map(int, input().split())
	ans = max(ans, d + c)
print(ans)


from sys import stdin
(n, s) = map(int, stdin.readline().split())
val = []
for i in range(n):
	(a, b) = map(int, stdin.readline().split())
	val.append([a, b])
val.sort(reverse=True)
ans = 0
stack = [s]
for i in val:
	nval = stack[-1]
	ans += nval - i[0]
	if i[1] > ans:
		ans += i[1] - ans
	stack.append(i[0])
print(ans + stack[-1])


(n, s) = map(int, input().split())
l = []
for i in range(n):
	(f, t) = map(int, input().split())
	l.append((f, t))
l.sort(key=lambda x: x[0], reverse=True)
i = 0
for j in range(n):
	i += s - l[j][0]
	i += max(0, l[j][1] - i)
	s = l[j][0]
i += s
print(i)


def solve(s, p):

	def solve_rec(t, s, p):
		if not p:
			return t + s
		(floor, when) = p.pop()
		return solve_rec(max(t + s - floor, when), floor, p)
	p.sort()
	return solve_rec(0, s, p)
(n, s) = map(int, input().split())
p = [list(map(int, input().split())) for _ in range(n)]
print(solve(s, p))


(n, s) = map(int, input().split())
lst = []
for i in range(n):
	lst.append(list(map(int, input().split())))
lst = sorted(lst, key=lambda x: x[0], reverse=True)
(prev, ans) = (s, 0)
for i in range(n):
	ans += prev - lst[i][0]
	if ans < lst[i][1]:
		ans += lst[i][1] - ans
	prev = lst[i][0]
print(ans + prev)


(n, s) = [int(i) for i in input().split()]
f = n * [0]
t = n * [0]
for i in range(n):
	(f[i], t[i]) = [int(i) for i in input().split()]
for i in range(n):
	bish = f[0]
	k = 0
	for j in range(1, n - i):
		if f[j] < bish:
			bish = f[j]
			k = j
	(f[k], f[n - 1 - i]) = (f[n - 1 - i], f[k])
	(t[k], t[n - 1 - i]) = (t[n - 1 - i], t[k])
tim = 0
t.append(0)
f.append(0)
ct = s
for i in range(n + 1):
	tim += ct - f[i]
	if tim < t[i]:
		tim = t[i]
	ct = f[i]
print(tim)


(a, b) = map(int, input().split())
print(max(b, max((sum(map(int, input().split())) for i in range(a)))))


(n, s) = map(int, input().split())
last = [0 for i in range(s + 1)]
for i in range(n):
	(f, t) = map(int, input().split())
	last[f] = max(last[f], t)
ans = -1
for i in range(s, -1, -1):
	ans += 1
	ans = max(ans, last[i])
print(ans)


(n, s) = map(int, input().split())
ip = []
count = 0
for i in range(n):
	(a, b) = map(int, input().split())
	ip.append((a, b))
ip = sorted(ip, key=lambda l: l[0], reverse=True)
for i in range(n):
	(a, b) = ip[i]
	count += s - a
	if count < b:
		count += b - count
	s = a
count += a
print(count)


def to_list(s):
	return list(map(lambda x: int(x), s.split(' ')))

def solve(s, stages, secs):
	sorted_ = sorted(zip(stages, secs), key=lambda x: x[0], reverse=True)
	curr_stage = s
	curr_time = 0
	for item in sorted_:
		lift_time = curr_stage - item[0]
		curr_time = curr_time + lift_time
		waiting_time = max(0, item[1] - curr_time)
		curr_time = curr_time + waiting_time
		curr_stage = item[0]
	return str(curr_time + curr_stage)
stages = []
secs = []
(n, s) = to_list(input())
for i in range(n):
	inp = to_list(input())
	stages.append(inp[0])
	secs.append(inp[1])
print(solve(s, stages, secs))


(n, f) = map(int, input().split())
a = []
for i in range(n):
	a.append([x for x in map(int, input().split())])
a.sort(reverse=True)
time = 0
for i in range(n):
	time += f - a[i][0]
	f = a[i][0]
	time += max(0, a[i][1] - time)
time += f
print(time)


(n, fl) = map(int, input().split())
z = []
for i in range(n):
	(f, t) = map(int, input().split())
	z.append((f, t))
s = 0
z = sorted(z, key=lambda x: x[0], reverse=True)
z.append((0, 0))
t = 0
for i in range(len(z)):
	t += abs(fl - z[i][0])
	t += z[i][1] - t if z[i][1] - t >= 0 else 0
	fl = z[i][0]
print(t)


def main():
	(n, s) = map(int, input().split())
	arrivals = []
	for i in range(n):
		(f, t) = map(int, input().split())
		arrivals.append((f, t))
	arrivals.sort(reverse=True)
	time = 0
	for (f, t) in arrivals:
		time += s - f
		if t > time:
			time = t
		s = f
	if s > 0:
		time += s
	print(time)
main()


import sys
fin = sys.stdin
(n, s) = map(int, fin.readline().split())
floor = [0] * s
for i in range(n):
	(fl_num, sec) = map(int, fin.readline().split())
	floor[fl_num - 1] = max(floor[fl_num - 1], sec)
ctime = 0
for i in range(s, 0, -1):
	ctime = max(ctime, floor[i - 1])
	ctime += 1
print(ctime)


(n, s) = map(int, input().split())
mayor = s
for i in range(n):
	mayor = max(sum(map(int, input().split())), mayor)
print(mayor)


x = list(map(int, input().split()))
floormax = x[1]
floors = []
times = []
for i in range(x[0]):
	a = list(map(int, input().split()))
	floors.append(a[0])
	times.append(a[1])
floors.reverse()
times.reverse()
totaltime = 0
lastfloor = floormax
for i in range(len(floors)):
	totaltime += lastfloor - floors[i]
	if times[i] > totaltime:
		totaltime += times[i] - totaltime
	lastfloor = floors[i]
totaltime += lastfloor
print(totaltime)


(n, s) = map(int, input().split())
data = []
for i in range(n):
	(level, time) = map(int, input().split())
	data.append((level, time))
data1 = sorted(data)[::-1]
during = 0
level = s
for i in range(n):
	during += level - data1[i][0]
	level = data1[i][0]
	if during < data1[i][1]:
		during += data1[i][1] - during
print(during + data1[-1][0])


(n, s) = map(int, input().split())
t = 0
f = [0] * n
v = [0] * n
for i in range(n):
	(f[i], v[i]) = map(int, input().split())
f.reverse()
v.reverse()
for i in range(n):
	t += s - f[i]
	t += max(0, v[i] - t)
	s = f[i]
t += f[-1]
print(t)


(n, top) = map(int, input().split())
r = lambda : list(map(int, input().split()))
arr = [r() for _ in range(n)]
arr.sort(reverse=True, key=lambda x: x[0])
arr.append([0, 0])
c = 0
for (i, j) in arr:
	c += top - i
	c += max(0, j - c)
	top = i
print(c)


d = list(map(int, input().split()))
flr = []
time = []
total = []
for i in range(0, d[0]):
	temp = list(map(int, input().split()))
	total.append(temp[0] + temp[1])
m1 = max(total)
print(max(m1, d[1]))


import sys
(n, s) = map(int, sys.stdin.readline().split())
ans = s
for _ in range(n):
	(f, t) = map(int, sys.stdin.readline().split())
	ans = max(ans, f + max(s - f, t))
print(ans)


from collections import defaultdict
(n, m) = map(int, input().split())
time = 0
current_floor = m
stops = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x: (-x[0], x[1]))
count = defaultdict(int)
for (f, arrive) in stops:
	time += current_floor - f
	time += max(0, arrive - time)
	current_floor = f
print(time + current_floor)


(n, m) = map(int, input().split())
a = []
t = 0
for i in range(n):
	(x, y) = map(int, input().split())
	a.append((x, y * -1))
a.sort()
a.reverse()
for i in a:
	i = (i.__getitem__(0), i.__getitem__(1) * -1)
	if m > i.__getitem__(0):
		t = t + m - i.__getitem__(0)
	if t < i.__getitem__(1):
		t = i.__getitem__(1)
	m = i.__getitem__(0)
print(t + m)


(a, b) = list(map(int, input().split()))
c = [b]
for i in range(a):
	(e, f) = list(map(int, input().split()))
	c += [e + f]
print(max(c))


line = list(map(int, input().split()))
n = line[0]
s = line[1]
floors = []
for i in range(0, n):
	line = list(map(int, input().split()))
	floors.append(line)
floors.sort(reverse=True)
t = 0
for k in floors:
	if k[0] < s:
		t = t + s - k[0]
		s = k[0]
	if t < k[1]:
		t = k[1]
if s != 0:
	t = t + s
print(t)


R = lambda : map(int, input().split())
(n, s) = R()
m = max([sum(R()) for _ in range(n)])
print(max(m, s))


x = input()
x = x.split()
b = []
suma = 0
diferencia = 0
tiempoa = 0
pisomax = int(x[1])
for k in range(int(x[0])):
	a = input()
	a = a.split()
	b.append(a)
for k in range(int(x[0])):
	b[k][1] = int(b[k][1])
	b[k][0] = int(b[k][0])
b.sort(reverse=True)
for k in range(int(x[0])):
	sumapisos = pisomax - b[k][0]
	tiempo = b[k][1] - sumapisos - tiempoa
	if tiempo < 0:
		tiempo = 0
	suma = suma + sumapisos + tiempo
	pisomax = b[k][0]
	tiempoa = sumapisos + tiempoa + tiempo
suma = suma + b[k][0]
print(suma)


(n, s) = map(int, input().split())
a = []
z = []
d = {}
for i in range(n):
	(f, t) = map(int, input().split())
	if f in d:
		if t > d[f]:
			d[f] = t
	else:
		d[f] = t
		z.append(f)
s1 = 0
z.sort(reverse=True)
k = 0
for i in range(s, 0, -1):
	if i in z:
		if d[z[k]] > s1:
			s1 = d[z[k]]
			s1 = s1 + 1
		else:
			s1 = s1 + 1
		k = k + 1
	else:
		s1 = s1 + 1
print(s1)


(n, s) = map(int, input().split())
ft = [list(map(int, input().split())) for i in range(n)]
maxi = s
for i in range(n):
	if maxi < ft[i][0] + ft[i][1]:
		maxi = ft[i][0] + ft[i][1]
print(maxi)


(n, s) = list(map(int, input().split(' ')))
l = [0] * (s + 1)
for i in range(n):
	(f, t) = list(map(int, input().split(' ')))
	if l[f] < t:
		l[f] = t
current = -1
for i in range(s, -1, -1):
	current += 1
	if current < l[i]:
		current = l[i]
print(current)


(n, s) = [int(x) for x in input().split()]
time = 0
floor = 0
maxeta = 0
vremya = 0
for i in range(n):
	(f, t) = [int(x) for x in input().split()]
	if time + floor < f + t:
		time = t
		floor = f
	if maxeta < floor:
		maxeta = floor
		vremya = t
z = 0
if s > maxeta and time + floor < s:
	print(s)
else:
	print(time + floor + z)


(n, k) = map(int, input().split())
for i in range(n):
	(a, b) = map(int, input().split())
	if k - a <= b:
		k += b - (k - a)
print(k)


A = input().split()
n = int(A[0])
s = int(A[1])
f = []
h = []
a = []
for i in range(0, n):
	B = input().split()
	f.append(B[0])
	h.append(B[1])
for i in range(0, n):
	if s - int(f[i]) < int(h[i]):
		a.append(int(h[i]) + int(f[i]))
	else:
		a.append(s)
a.sort()
print(a[-1])


(n, m) = map(int, input().split())
ls = []
for i in range(n):
	(f, t) = map(int, input().split())
	tp = (f, t)
	ls.append(tp)
time = 0
ls.sort()
ls = ls[::-1]
time = m - ls[0][0]
m = ls[0][0]
for i in ls:
	time = time + m - i[0]
	if time >= i[1]:
		pass
	else:
		time = i[1]
	m = i[0]
time = time + ls[n - 1][0]
print(time)


(n, s) = map(int, input().split())
ans = s
f = []
t = []
for i in range(n):
	(f, t) = map(int, input().split())
	if t > s - f:
		ans += t - (s - f)
		s += t - (s - f)
print(ans)


(n, s) = input().strip().split(' ')
(n, s) = [int(n), int(s)]
f = []
t = []
for a0 in range(n):
	(x, y) = input().strip().split(' ')
	(x, y) = [int(x), int(y)]
	f.append(x)
	t.append(y)
time = 0
cur_flr = s
for i in range(n):
	time += cur_flr - f[n - 1 - i]
	if time < t[n - 1 - i]:
		time = t[n - 1 - i]
	cur_flr = f[n - 1 - i]
if cur_flr:
	time += cur_flr
print(time)


(n, s) = map(int, input().split())
d = {}
for _ in range(n):
	(f, t) = map(int, input().split())
	d[f] = max(d.get(f, 0), t)
r = -1
for i in range(s, -1, -1):
	r = max(r + 1, d.get(i, 0))
print(r)


read = lambda : map(int, input().split())
(n, s) = read()
ans = s
for i in range(n):
	(f, t) = read()
	ans = max(ans, f + t)
print(ans)


from operator import add

def main():
	(n, s) = map(int, input().split())
	print(max(max((add(*map(int, input().split())) for _ in range(n))), s))
main()


from collections import deque, defaultdict, Counter
from itertools import product, groupby, permutations, combinations
from math import gcd, floor, inf
from bisect import bisect_right, bisect_left
(cases, s) = map(int, input().split())
passengers = []
prev_time = 0
for _ in range(cases):
	(f, t) = map(int, input().split())
	passengers.append([f, t])
passengers = sorted(passengers, reverse=True)
ans = s
prev_flr = s
for (fi, ti) in passengers:
	elev_arrive = prev_time + prev_flr - fi
	prev_flr = fi
	if ti > elev_arrive:
		ans += ti - elev_arrive
	prev_time = max(elev_arrive, ti)
print(ans)


(n, s) = map(int, input().split())
ans = s
for _ in range(n):
	(f, t) = map(int, input().split())
	ans = max(ans, f + t)
print(ans)


(n, s) = list(map(int, input().split()))
ps = []
for i in range(n):
	(f, t) = list(map(int, input().split()))
	ps.append((f, t))
ps.sort(reverse=True)
ans = 0
nowf = s
for (f, t) in ps:
	ans += nowf - f
	nowf = f
	ans = t if t - ans > 0 else ans
ans += nowf
print(ans)


(p, n) = map(int, input().split(' '))
l = []
for i in range(p):
	(f1, t1) = map(int, input().split(' '))
	l.append((f1, t1))
l.sort()
floor = n
sumi = 0
x = (0, 0)
while not len(l) == 0:
	x = l.pop()
	sumi += floor - x[0]
	if sumi < x[1]:
		sumi += x[1] - sumi
	floor = x[0]
if x[0] != 0:
	sumi += x[0]
print(sumi)


import sys
(a, b) = input().split()
li = [0 for x in range(1000 + 1)]
a = int(a)
b = int(b)
for x in range(a):
	(c, d) = input().split()
	c = int(c)
	d = int(d)
	if li[c] < d:
		li[c] = d
ans = 0
fl = b
while fl > 0:
	if ans < li[fl]:
		ans = li[fl]
	fl -= 1
	ans += 1
print(ans)
sys.exit()


(n, s) = map(int, input().split())
t = [0] * (s + 1)
for i in range(n):
	(a, b) = map(int, input().split())
	if t[a] < b:
		t[a] = b
answer = 0
lastfloor = s
for i in range(s, -1, -1):
	if t[i] > 0:
		answer += lastfloor - i
		if answer < t[i]:
			answer = t[i]
		lastfloor = i
print(answer + lastfloor)


(n, s) = map(int, input().split())
floors = [0 for i in range(s + 1)]
time = 0
for i in range(n):
	(f, t) = map(int, input().split())
	floors[f] = max(floors[f], t)
time = max(time, floors[s])
for i in range(s - 1, -1, -1):
	time += 1
	time = max(time, floors[i])
print(time)


string = input()
numbers = string.split()
(a, b, floors, times) = (int(numbers[0]), int(numbers[1]), [], [])
for x in range(a):
	string = input()
	numbers = string.split()
	floors.append(int(numbers[0]))
	times.append(int(numbers[1]))
(floors, times) = (floors[::-1], times[::-1])
n = 0
for x in range(a):
	n += b - floors[x]
	b = floors[x]
	if times[x] > n:
		n = times[x]
n += b
print(n)


def solve(n, p, s):
	p.append((0, 0))
	p.sort()
	t = 0
	while p:
		x = p.pop()
		(s, t) = (x[0], max(x[1], t + abs(s - x[0])))
	return t
(n, s) = [int(x) for x in input().split(' ')]
p = [tuple([int(x) for x in input().split(' ')]) for r in range(n)]
print(solve(n, p, s))


(n, s) = map(int, input().split())
i = sorted([list(map(int, input().split())) for _ in range(n)])[::-1]
c = 0
for x in i:
	(f, t) = (x[0], x[1])
	c += max(s - f, x[1] - c)
	s = f
print(c + s)


(n, s) = map(int, input().split())
p = sorted([list(map(int, input().split())) for i in range(n)])
time = s
for i in p:
	time += max(0, i[1] - time + i[0])
print(time)


__author__ = 'MoonBall'
import sys
T = 1

def process():
	(N, S) = list(map(int, input().split()))
	a = [-1] * 1100
	for _ in range(N):
		(f, t) = list(map(int, input().split()))
		a[f] = max(a[f], t)
	ans = 0
	for i in range(S, -1, -1):
		if i != S:
			ans = ans + 1
		if a[i] != -1 and a[i] > ans:
			ans = a[i]
	print(ans)
for _ in range(T):
	process()


(n, s) = [int(x) for x in input().split()]
timearray = [0 for i in range(s + 1)]
for i in range(n):
	(f, t) = [int(x) for x in input().split()]
	if t > timearray[f]:
		timearray[f] = t
currenttime = 0
for currentfloor in range(s, 0, -1):
	timeofarrival = timearray[currentfloor]
	if timeofarrival == 0:
		currenttime += 1
	elif timeofarrival < currenttime:
		currenttime += 1
	else:
		currenttime = timeofarrival + 1
print(currenttime)


(n, s) = map(int, input().split())
l = []
for i in range(n):
	l.append(tuple(map(int, input().split())))
res = 0
for (p, t) in l[::-1]:
	res += s - p
	s = p
	if t - res > 0:
		res += t - res
res += l[0][0]
print(res)


(x, f) = list(map(int, input().split()))
floor = []
time = []
for i in range(x):
	(n, m) = input().split()
	floor.append(int(n))
	time.append(int(m))
floor.reverse()
time.reverse()
floor.append(int(0))
i = 0
t = 0
if floor[i] < f:
	t = f - floor[i]
while i < len(time):
	if time[i] > t:
		t += time[i] - t
	t += floor[i] - floor[i + 1]
	i += 1
print(t)


(n, s) = [int(i) for i in input().split()]
l = []
m = []
sum = 0
for i in range(n):
	(a, b) = [int(i) for i in input().split()]
	l.append(a)
	m.append(b)
while len(l) > 0:
	q = max(l)
	b = l.index(q)
	sum = sum + (s - q)
	s = q
	if sum >= m[b]:
		pass
	else:
		sum = sum + (m[b] - sum)
	if len(l) == 1 and l[0] != 0:
		sum = sum + l[0]
	l.remove(q)
	m.pop(b)
print(sum)


(n, s) = map(int, input().split())
mins = s
my_dict = {}
mylist = []
while n:
	(person, floor) = map(int, input().split())
	mylist.append(person + floor)
	n -= 1
val = max(mylist)
if val < mins:
	print(mins)
else:
	print(val)


(n, s) = map(int, input().split())
arr = [0 for _ in range(s + 1)]
for _ in range(n):
	(f, t) = map(int, input().split())
	arr[f] = max(arr[f], t)
arr.reverse()
cnt = arr[0]
for x in arr[1:]:
	cnt = max(x, cnt + 1)
print(cnt)


import sys
(n, top) = map(int, input().split())
floor = [0] * n
arrival = [0] * n
time = 0
for i in range(n):
	(f, a) = map(int, input().split())
	floor[i] = f
	arrival[i] = a
for i in range(n - 1, -1, -1):
	time += top - floor[i]
	if arrival[i] > time:
		time += arrival[i] - time
	top = floor[i]
	if i == 0 and floor[i] > 0:
		time += top
print(time)


(n, p) = map(int, input().split())
print(max([sum(map(int, input().split())) for i in range(n)] + [p]))


from sys import stdin
(n, s1) = map(int, stdin.readline().strip().split())
s = [list(map(int, stdin.readline().strip().split())) for i in range(n)]
s.sort(reverse=True)
s.append([0, 0])
st = 0
l = s1
t = 0
for i in range(n):
	t += l - s[i][0]
	if t < s[i][1]:
		t = s[i][1]
	l = s[i][0]
t += l - 0
print(t)


(n, s) = map(int, input().split())
z = 0
for i in range(n):
	(f, t) = map(int, input().split())
	su = f + t
	z = max(z, su)
print(max(z, s))


import sys
import math

def main():
	a = [list(map(int, x.split())) for x in sys.stdin]
	inf = a.pop(0)
	a.sort()
	a.reverse()
	seconds = inf[1] - a[0][0]
	if a[0][1] >= seconds:
		seconds += a[0][1] - seconds
	for x in range(0, len(a) - 1, 1):
		seconds += a[x][0] - a[x + 1][0]
		if a[x + 1][1] - seconds >= 1:
			seconds += a[x + 1][1] - seconds
	seconds += a[-1][0]
	print(seconds)
main()


(n, s) = map(int, input().split())
ans = 0
p = []
for i in range(n):
	p.append(list(map(int, input().split())))
p.sort()
for i in range(n):
	ans += s - p[-i - 1][0]
	s = p[-i - 1][0]
	ans += max(0, p[-i - 1][1] - ans)
print(ans + s)


(n, s) = map(int, input().split())
floors = []
for i in range(n):
	floors.append(list(map(int, input().split())))
floors = sorted(floors, key=lambda x: x[0], reverse=True)
t = 0
for i in range(n):
	if s > floors[i][0]:
		t += s - floors[i][0]
	if t < floors[i][1]:
		t += floors[i][1] - t
	s = floors[i][0]
if s > 0:
	t += abs(0 - s)
print(t)


def find_biggest(n, ps, s):
	n -= 1
	i = 0
	m = 0
	while n >= 0 and ps[n][0] == s:
		i = n
		m = max(m, ps[n][1])
		n -= 1
	return (m, i)

def calc(n, ps, s, time):
	if n == 0:
		return s
	if s == 0:
		return 0
	if ps[n - 1][0] == s:
		(biggest, i) = find_biggest(n, ps, s)
		if time >= biggest:
			return 1 + calc(i, ps[:i], s - 1, time + 1)
		return biggest + calc(i, ps[:i], s - 1, biggest + 1) - time + 1
	else:
		return s - ps[n - 1][0] + calc(n, ps, ps[n - 1][0], time + s - ps[n - 1][0])

def input_int_array():
	return [int(i) for i in input().split()]
(n, s) = input_int_array()
ps = []
for i in range(n):
	ps.append(input_int_array())
ps.sort(key=lambda a: a[0])
print(calc(n, ps, s, 0))


(s, n) = [int(i) for i in input().split()]
out = 0
for j in range(s):
	(a, b) = [int(i) for i in input().split()]
	out += n - a
	out = max(out, b)
	n = a
out += n
print(out)


(n, s) = [int(x) for x in input().split()]
a = []
for i in range(n):
	(floor, time) = [int(x) for x in input().split()]
	a += [[floor, time]]
a.sort(reverse=True)
currfloor = s
time = 0
for i in a:
	time += currfloor - int(i[0])
	if time < i[1]:
		time += i[1] - time
	currfloor = i[0]
time += currfloor
print(time)


(n, s) = (int(i) for i in input().split())
maxi = s
for _ in range(n):
	(a, b) = (int(i) for i in input().split())
	if a + b > maxi:
		maxi = a + b
print(maxi)


(a, b) = map(int, input().split())
i = -1
t1 = 0
ff = 0
for t in range(a):
	(f, t) = map(int, input().split())
	i += 1
	if t + f >= t1:
		t1 = t + f
if t1 >= b:
	print(t1 + ff)
else:
	print(b)


(n, s) = map(int, input().split(' '))
ls = []
for _ in range(n):
	ls.append(list(map(int, input().split(' '))))
for i in range(n):
	s = max(s, sum(ls[i]))
print(s)


__author__ = 'sandeepmellacheruvu'
(n, s) = map(int, input().split())
arr = []
for i in range(n):
	(f, t) = map(int, input().split())
	arr.append((f, t))
curTime = 0
arr.sort(reverse=True)
prevFloor = s
for i in range(n):
	ele = arr[i]
	if prevFloor - ele[0] > 0:
		curTime += prevFloor - ele[0]
	timeChange = ele[1] - curTime
	if timeChange > 0:
		curTime += timeChange
	prevFloor = ele[0]
print(curTime + prevFloor)


(n, s) = map(int, input().split())
L = []
M = []
for i in range(n):
	(f, t) = map(int, input().split())
	L.append((f, t))
	M.append((f, t))
L.sort()
M.sort()
now = s
time = 0
while len(L) != 0:
	if L[-1][0] == now:
		time = max(time, L[-1][1])
		L.pop()
	else:
		now -= 1
		time += 1
print(time + M[0][0])


import time
(n, s) = (int(i) for i in input().split())
ft = [sum([int(i) for i in input().split()]) for _ in range(n)]
start = time.time()
print(max(max(ft), s))
finish = time.time()


(n, k) = map(int, input().split())
lst = []
for i in range(n):
	(c, v) = map(int, input().split())
	lst.append([c, v])
lst.sort(reverse=1)
sum = 0
sum += k - lst[0][0]
if lst[0][1] - sum > 0:
	sum += lst[0][1] - sum
for i in range(1, n):
	sum += lst[i - 1][0] - lst[i][0]
	if lst[i][1] - sum > 0:
		sum += lst[i][1] - sum
print(sum + lst[i][0])