The girl Taylor has a beautiful calendar for the year y. In the calendar all days are given with their days of week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.

The calendar is so beautiful that she wants to know what is the next year after y when the calendar will be exactly the same. Help Taylor to find that year.

Note that leap years has 366 days. The year is leap if it is divisible by 400 or it is divisible by 4, but not by 100 (https://en.wikipedia.org/wiki/Leap_year).


-----Input-----

The only line contains integer y (1000 ≤ y < 100'000) — the year of the calendar.


-----Output-----

Print the only integer y' — the next year after y when the calendar will be the same. Note that you should find the first year after y with the same calendar.


-----Examples-----
Input
2016

Output
2044

Input
2000

Output
2028

Input
50501

Output
50507



-----Note-----

Today is Monday, the 13th of June, 2016.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Implementation


Solutions:
import sys, math

def isleap(y):
	if y % 400 == 0:
		return True
	elif y % 4 == 0 and y % 100 != 0:
		return True
	else:
		return False
y = int(input())
x = 0
ans = y
while True:
	if isleap(y):
		x += 2
	else:
		x += 1
	x %= 7
	y += 1
	if x == 0 and isleap(y) == isleap(ans):
		break
print(y)


def isLeapYear(year):
	return year % 400 == 0 or (year % 4 == 0 and (not year % 100 == 0))
year = int(input())
newYear = year
isLeap = isLeapYear(year)
offset = 0
while True:
	offset = (offset + 365 + 1 * isLeapYear(newYear)) % 7
	newYear += 1
	if offset == 0 and isLeap == isLeapYear(newYear):
		print(newYear)
		break
	if newYear > year + 2000:
		print("couldn't find year")
		break


def f(n):
	if n % 400 == 0 or (n % 100 != 0 and n % 4 == 0):
		return 2
	else:
		return 1
n = int(input())
x = n
c = f(n)
n += 1
while c % 7 or f(n) != f(x):
	c += f(n)
	n += 1
print(n)


import math
n = int(input())

def leap(n):
	if n % 4 == 0 and n % 100 != 0 or n % 400 == 0:
		return 2
	else:
		return 1

def years(n):
	extra = leap(n)
	total = 0
	for i in range(50):
		n += 1
		total += leap(n)
		total %= 7
		if total % 7 == 0 and leap(n) == extra:
			print(n)
			exit(0)
years(n)


y = int(input())
b = 0
y0 = y
while b or y == y0 or ((y0 % 400 == 0 or (y0 % 4 == 0 and y0 % 100)) and (not (y % 400 == 0 or (y % 4 == 0 and y % 100)))) or (not (y0 % 400 == 0 or (y0 % 4 == 0 and y0 % 100)) and (y % 400 == 0 or (y % 4 == 0 and y % 100))):
	b += 365 % 7
	if y % 400 == 0 or (y % 4 == 0 and y % 100):
		b += 1
	y += 1
	b %= 7
print(y)


def year(n):
	if n % 400 == 0 or (n % 4 == 0 and n % 100 != 0):
		return 2
	return 1
now = int(input())
s = now
balance = 0
while balance % 7 != 0 or balance == 0 or year(now) != year(s):
	now += 1
	balance += year(now)
print(now)


__author__ = 'zihaozhu'
from sys import stdin

def is_leap(year):
	return year % 400 == 0 or (year % 100 != 0 and year % 4 == 0)
year = int(stdin.readline())
newyear = year
delta = 0
while year == newyear or not (delta == 0 and is_leap(year) == is_leap(newyear)):
	delta += 365 + int(is_leap(newyear))
	delta %= 7
	newyear += 1
print(newyear)


year = int(input())
f = lambda y: 2 if y % 4 == 0 and y % 100 != 0 or y % 400 == 0 else 1
old = f(year)
offset = f(year)
year += 1
while offset or old != f(year):
	offset += f(year)
	offset %= 7
	year += 1
print(year)


def vis(k):
	if k % 400 == 0:
		return True
	if k % 4 == 0 and k % 100 != 0:
		return True
	return False
n = int(input())
v = vis(n)
if v == True:
	s = 366
else:
	s = 365
i = n + 1
while s % 7 != 0 or vis(i) != v:
	s += 365
	if vis(i):
		s += 1
	i += 1
print(i)


def is_leap(Y):
	return Y % 4 == 0 and (Y % 100 != 0 or Y % 400 == 0)

def next(y, d, l):
	d += 1
	if l:
		d += 1
	d = d % 7
	y += 1
	l = is_leap(y)
	return (y, d, l)
S = int(input())
L = is_leap(S)
(y, d, l) = next(S, 0, L)
while d != 0 or l != L:
	(y, d, l) = next(y, d, l)
print(y)


def main():

	def leap(y):
		return y % 100 and (not y % 4) or not y % 400
	n = int(input())
	a = leap(n)
	(b, x) = (not a, 0)
	while a != b or x % 7:
		n += 1
		b = leap(n)
		x += 365 + b
	print(n)
main()


y = int(input())
if y % 4 == 0 and y % 100 != 0 or (y % 4 == 0 and y % 100 == 0 and (y % 400 == 0)):
	leap = 1
else:
	leap = 0
cd = 0
while 1:
	cy = y + 1
	if y % 4 == 0 and y % 100 != 0 or (y % 4 == 0 and y % 100 == 0 and (y % 400 == 0)):
		cd += 2
	else:
		cd += 1
	cd = cd % 7
	if cy % 4 == 0 and cy % 100 != 0 or (cy % 4 == 0 and cy % 100 == 0 and (cy % 400 == 0)):
		cl = 1
	else:
		cl = 0
	if cd == 0 and cl == leap:
		break
	y += 1
print(cy)


def vis(n):
	return 2 if n % 4 == 0 and n % 100 or n % 400 == 0 else 1
(n, o) = (int(input()), 0)
nn = n
while True:
	o += vis(n)
	n += 1
	if o % 7 == 0 and vis(n) == vis(nn):
		break
print(n)


def year(x):
	if x % 400 == 0:
		return 2
	elif x % 4 == 0 and x % 100 != 0:
		return 2
	else:
		return 1
a = int(input())
b = a
s = year(a)
while True:
	a = a + 1
	s = s + year(a)
	if s % 7 == 0 and year(b) == year(a + 1):
		print(a + 1)
		break


def days(year):
	if year % 400 == 0:
		return 366
	if year % 4 == 0 and year % 100 != 0:
		return 366
	return 365
year = int(input())
f = days(year)
t = days(year) % 7
year += 1
while True:
	if t == 0 and days(year) == f:
		print(year)
		exit()
	t += days(year)
	t %= 7
	year += 1


import sys
import bisect
from bisect import bisect_left as lb
input_ = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
from math import atan2, acos
from random import randint
sa = lambda : input_()
sb = lambda : int(input_())
sc = lambda : input_().split()
sd = lambda : list(map(int, input_().split()))
sflo = lambda : list(map(float, input_().split()))
se = lambda : float(input_())
sf = lambda : list(input_())
flsh = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7
gp = []
cost = []
dp = []
mx = []
ans1 = []
ans2 = []
special = []
specnode = []
a = 0
kthpar = []

def dfs(root, par):
	if par != -1:
		dp[root] = dp[par] + 1
	for i in range(1, 20):
		if kthpar[root][i - 1] != -1:
			kthpar[root][i] = kthpar[kthpar[root][i - 1]][i - 1]
	for child in gp[root]:
		if child == par:
			continue
		kthpar[child][0] = root
		dfs(child, root)
ans = 0

def hnbhai(t):
	y = sb()
	leap = 1 if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0) else 0
	d = 0
	while True:
		d += 1
		d %= 7
		y += 1
		isleap = 1 if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0) else 0
		if isleap:
			d += 1
			d %= 7
		if d == 0 and leap == isleap:
			break
	print(y)
for _ in range(1):
	hnbhai(_ + 1)


def judge(x):
	if x % 400 == 0:
		return 2
	if x % 4 == 0 and x % 100 != 0:
		return 2
	else:
		return 1
x = int(input())
ans = 0
flag = judge(x)
x += 1
while True:
	ans += judge(x)
	if ans % 7 == 0:
		if flag == judge(x):
			print(x)
			break
		else:
			x += 1
	else:
		x += 1


def isLeap(x):
	if x % 400 == 0:
		return True
	elif x % 4 == 0 and x % 100 != 0:
		return True
	return False
x = int(input())
y = x + 1
diff = 365 + isLeap(y)
while diff % 7 or isLeap(y) != isLeap(x):
	y += 1
	diff += 365 + isLeap(y)
print(y)


def is_leap(y):
	if y % 400 == 0:
		return True
	if y % 100 == 0:
		return False
	if y % 4 == 0:
		return True
	return False
y = int(input())
y_ = y
r = 0
while True:
	if is_leap(y_):
		r = (r + 366) % 7
	else:
		r = (r + 365) % 7
	y_ += 1
	if r == 0 and is_leap(y) == is_leap(y_):
		print(y_)
		break


n = int(input())
count = 0
flag = True
if n % 4 == 0 and n % 100 != 0 or n % 400 == 0:
	flag = False
while True:
	n += 1
	if n % 400 == 0:
		count += 2
	elif n % 4 == 0 and n % 100 != 0:
		count += 2
	else:
		count += 1
	if count % 7 == 0:
		if flag == False and (n % 4 == 0 and n % 100 != 0 or n % 400 == 0):
			print(n)
			break
		elif flag == True and ((n % 4 != 0 or n % 100 == 0) and n % 400 != 0):
			print(n)
			break
		else:
			continue


def leap(y):
	return y % 400 == 0 or (y % 4 == 0 and y % 100 != 0)
(sun, mon, tue, wed, thu, fri, sat) = [i for i in range(7)]
l = {i: 0 for i in range(1000, 110000)}
l[2016] = fri
for i in range(2015, 999, -1):
	if leap(i):
		l[i] = (7 + l[i + 1] - 2) % 7
	else:
		l[i] = (7 + l[i + 1] - 1) % 7
for i in range(2017, 110000):
	if leap(i - 1):
		l[i] = (l[i - 1] + 2) % 7
	else:
		l[i] = (l[i - 1] + 1) % 7
year = int(input())
for i in range(year + 1, 110000):
	if l[i] == l[year] and leap(i) == leap(year):
		print(i)
		break


day = 0
y = int(input())
leap = y % 400 == 0 or (y % 4 == 0 and y % 100 != 0)
y += 1
days = 366 % 7 if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0) else 365 % 7
day = (day + days) % 7
while True:
	if day == 0:
		this_leap = y % 400 == 0 or (y % 4 == 0 and y % 100 != 0)
		if leap == this_leap:
			break
	y += 1
	days = 366 % 7 if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0) else 365 % 7
	day = (day + days) % 7
print(y)


def dias_anho(a):
	if a % 4 == 0:
		if a % 100 == 0:
			if a % 400 == 0:
				d = 366
			else:
				d = 365
		else:
			d = 366
	else:
		d = 365
	return d
a = int(input(''))
dias_a = dias_anho(a)
dia = 1
a1 = a + 1
dias_a1 = dias_anho(a1)
dia1 = (dias_anho(a1) % 7 + dia) % 7
while dias_a != dias_a1 or dia != dia1:
	a1 = a1 + 1
	dias_a1 = dias_anho(a1)
	dia1 = (dias_anho(a1) % 7 + dia1) % 7
print(a1)


__metaclass__ = type
__author__ = 'xdlove'
n = int(input())
flag = (n % 400 == 0 or (n % 4 == 0 and n % 100 != 0)) + 365
res = 0
while True:
	n += 1
	tp = 365
	if n % 400 == 0 or (n % 4 == 0 and n % 100 != 0):
		tp = 366
	res = (res + tp % 7) % 7
	if res == 0 and tp == flag:
		break
print(n)


def visocosny(year):
	if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
		return True
	else:
		return False
y = int(input())
day = 1
year = y
if visocosny(year):
	match = True
else:
	match = False
f = True
while day != 1 or f:
	f = False
	year += 1
	if visocosny(year):
		day += 5
	else:
		day += 6
	day %= 7
	if day == 1 and (match and (not visocosny(year)) or (not match and visocosny(year))):
		f = True
print(year)


a = int(input())
if a % 4 == 0 and a % 100 != 0 or a % 400 == 0:
	R = True
else:
	R = False
d = 0
for i in range(a + 1, int(1000000000.0)):
	if i % 4 == 0 and i % 100 != 0 or i % 400 == 0:
		r = True
		d += 2
	else:
		r = False
		d += 1
	if R == r and d % 7 == 0:
		print(i)
		break


a = int(input())
aa = a
c = 0
if a % 400 == 0 or (a % 100 != 0 and a % 4 == 0):
	c = 366
else:
	c = 365
a += 1
while c % 7 != 0 or (a % 400 == 0 or (a % 100 != 0 and a % 4 == 0)) != (aa % 400 == 0 or (aa % 100 != 0 and aa % 4 == 0)):
	if a % 400 == 0 or (a % 100 != 0 and a % 4 == 0):
		c += 366
	else:
		c += 365
	a += 1
print(a)


def leap(y):
	return y % 400 == 0 or (y % 4 == 0 and y % 100 != 0)
y = int(input())
is_leap = leap(y)
d = 0
first = 1
while is_leap != leap(y) or d or first:
	d += 1
	if leap(y):
		d += 1
	y += 1
	d %= 7
	first = 0
print(y)


def number_of_days(year):
	if year % 400 == 0:
		return 366
	if year % 4 == 0 and year % 100 != 0:
		return 366
	return 365
y = int(input())
number_of_days_in_y = number_of_days(y)
total = number_of_days(y)
while True:
	if total % 7 == 0 and number_of_days_in_y == number_of_days(y + 1):
		print(y + 1)
		break
	y += 1
	total += number_of_days(y)


def leap(y):
	return y % 400 == 0 or (y % 100 != 0 and y % 4 == 0)
y = int(input())
isLeap = leap(y)
d = 0
while True:
	if leap(y):
		d += 2
	else:
		d += 1
	y += 1
	d %= 7
	if d == 0 and isLeap == leap(y):
		break
print(y)


y = int(input())
x = 0
if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):
	while True:
		y += 4
		if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):
			x += 5
		else:
			x += 4
			continue
		if x % 7 == 0:
			print(y)
			break
else:
	while True:
		y += 1
		if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):
			x += 2
			continue
		else:
			x += 1
		if x % 7 == 0:
			print(y)
			break


def isleap(n):
	return n % 400 == 0 or (n % 4 == 0 and n % 100 != 0)
x = int(input())
y = x + 1
day = 366 if isleap(x) else 365
day %= 7
while day != 0 or isleap(x) != isleap(y):
	day += 366 if isleap(y) else 365
	day %= 7
	y += 1
print(y)


def leap(x):
	return x % 400 == 0 or (x % 4 == 0 and x % 100 != 0)
(y, v) = (int(input()), 0)
l = leap(y)
while True:
	v = (v + 1 + leap(y)) % 7
	y += 1
	if v == 0 and leap(y) == l:
		print(y)
		break


from math import gcd

def getDays(n):
	if n % 100 == 0 and n % 400 == 0:
		return 366
	if n % 100 != 0 and n % 4 == 0:
		return 366
	return 365
y = int(input())
v = getDays(y)
sd = getDays(y)
while True:
	y += 1
	if getDays(y) == v and sd % 7 == 0:
		print(y)
		break
	sd += getDays(y)


def leap(n):
	return n % 400 == 0 or (n % 4 == 0 and n % 100 != 0)
y = int(input())
l = leap(y)
found = False
d = 0
while not found:
	if leap(y):
		d += 366
	else:
		d += 365
	y += 1
	if d % 7 == 0 and leap(y) == l:
		found = True
print(y)


d = lambda y: (y * 365 + y // 4 - y // 100 + y // 400) % 7
y = int(input())
(t, tt) = (d(y - 1), d(y) - d(y - 1))
while True:
	y += 1
	if d(y - 1) == t and d(y) - d(y - 1) == tt:
		print(y)
		break


y = int(input())
leap = y % 100 != 0 and y % 4 == 0 or y % 400 == 0
m = 0
for j in range(y, 10 ** 6):
	if j % 100 != 0 and j % 4 == 0 or j % 400 == 0:
		m += 2
	else:
		m += 1
	if m % 7 == 0 and leap == ((j + 1) % 100 != 0 and (j + 1) % 4 == 0 or (j + 1) % 400 == 0):
		print(j + 1)
		exit()


import datetime
y = int(input())
y_temp = y % 400
if y_temp == 0:
	print(y + 28)
else:
	y_2_28 = datetime.datetime(y_temp, 2, 28).weekday()
	y_3_1 = datetime.datetime(y_temp, 3, 1).weekday()
	count = 1
	while datetime.datetime(y_temp + count, 2, 28).weekday() != y_2_28 or datetime.datetime(y_temp + count, 3, 1).weekday() != y_3_1:
		count += 1
	print(y + count)


def check_leap_year(year):
	if year % 400 == 0:
		return True
	elif year % 4 == 0 and year % 100 != 0:
		return True
	else:
		return False
y = int(input())
year = y
leap_year_status = check_leap_year(year)
if check_leap_year(year + 1):
	curr_sum = 366
else:
	curr_sum = 365
current_leap_year_status = not leap_year_status
year += 1
while curr_sum % 7 != 0 or check_leap_year(year) != leap_year_status:
	if check_leap_year(year + 1):
		curr_sum += 366
	else:
		curr_sum += 365
	year += 1
print(year)


def isleap(n):
	if n % 4 == 0:
		if n % 100 == 0:
			if n % 400 == 0:
				return True
			return False
		return True
	return False
n = int(input())
a = 0
for i in range(n + 1, n + 10000000):
	if isleap(i):
		a += 2
	else:
		a += 1
	a = a % 7
	if a == 0:
		if isleap(n):
			if isleap(i):
				print(i)
				break
		elif isleap(i):
			continue
		else:
			print(i)
			break


def vis(n):
	if n % 400 == 0 or (n % 4 == 0 and n % 100 != 0):
		return True
	else:
		return False
n = int(input())
firstn = n
c = 1
flag = False
while c % 7 != 1 or flag == False or 2 * vis(n) * vis(firstn) != vis(n) + vis(firstn):
	if vis(n):
		d = 366
	else:
		d = 365
	c += d
	flag = True
	n += 1
print(n)


n = int(input())
startDay = 0
leap_n = n % 4 == 0 and n % 100 != 0 or n % 400 == 0
y = n
leap = leap_n
while startDay != 0 or leap != leap_n or y - n < 1:
	startDay = (startDay + 1 + leap) % 7
	y += 1
	leap = y % 4 == 0 and y % 100 != 0 or y % 400 == 0
print(y)


n = int(input())
num = 0
if n % 400 == 0 or (n % 4 == 0 and n % 100 != 0):
	num = 1
n2 = n + 1
num2 = 365 + num
while True:
	num3 = 0
	if n2 % 400 == 0 or (n2 % 4 == 0 and n2 % 100 != 0):
		num3 = 1
	if num2 % 7 == 0:
		if num3 == num:
			break
	num2 += 365 + num3
	n2 += 1
print(n2)


def main():
	year = int(input())
	found = False
	currentYear = year
	firstIsLeap = isLeap(year)
	total = 0
	while not found:
		total += 2 if isLeap(currentYear) else 1
		currentYear += 1
		if total % 7 == 0 and isLeap(currentYear) is firstIsLeap:
			found = True
	print(currentYear)

def isLeap(y):
	return y % 400 == 0 or (y % 4 == 0 and y % 100 != 0)
main()


y = int(input())
r = 0

def getDays(year):
	if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
		return 366
	else:
		return 365
days = getDays(y)
while r == 0 or not (r % 7 == 0 and getDays(y) == days):
	r += getDays(y)
	y += 1
print(y)


def is_visok(x):
	return x % 400 == 0 or (x % 4 == 0 and x % 100 != 0)
y = int(input())
(d, v) = (0, is_visok(y))
year = y
d_ = d
v_ = not v
while not (v_ == v and d_ % 7 == 0):
	year += 1
	v_ = is_visok(year)
	d_ += (365, 366)[v_] % 7
print(year)


def ka(n):
	if n % 400 == 0:
		return True
	elif n % 100 == 0:
		return False
	elif n % 4 == 0:
		return True
	else:
		return False
n = int(input())
x = n
c = 0
while True:
	n += 1
	if ka(n):
		c += 2
	else:
		c += 1
	if c % 7 == 0 and ka(n) == ka(x):
		break
print(n)


n = int(input())

def isLeapYear(a):
	return a % 400 == 0 or (a % 4 == 0 and a % 100 != 0)
a = n
if isLeapYear(n):
	days = 366
else:
	days = 365
a += 1
while not (days % 7 == 0 and isLeapYear(a) == isLeapYear(n)):
	if isLeapYear(a):
		days += 366
	else:
		days += 365
	a += 1
print(a)


import sys
from bisect import bisect_right as rb
from collections import deque
from queue import PriorityQueue
from math import *
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_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7
n = ii()
t = 0
if n % 100 != 0 and n % 4 == 0 or n % 400 == 0:
	t += 1
c = 0
for i in range(n, 10 ** 6):
	if i % 100 != 0 and i % 4 == 0 or i % 400 == 0:
		c += 2
	else:
		c += 1
	if c % 7 == 0 and t == ((i + 1) % 100 != 0 and (i + 1) % 4 == 0 or (i + 1) % 400 == 0):
		print(i + 1)
		exit(0)


y = int(input())

def is_leap(y):
	return y % 400 == 0 or (y % 4 == 0 and y % 100 != 0)

def days(y):
	return 365 if not is_leap(y) else 366
shift = days(y) % 7
for ny in range(y + 1, 200000):
	if is_leap(ny) == is_leap(y) and shift == 0:
		print(ny)
		break
	else:
		shift = (shift + days(ny)) % 7


y = int(input())
y -= 1
tot = 365 * y + (y // 4 - y // 100 + y // 400)
rem = tot % 7
pv = 0
if (y + 1) % 4 == 0 and ((y + 1) % 100 != 0 or (y + 1) % 400 == 0):
	pv = 1
y += 1
for i in range(y, 1000000):
	if i % 4 == 0 and (i % 100 != 0 or i % 400 == 0):
		tot += 366
	else:
		tot += 365
	cur = 0
	if (i + 1) % 4 == 0 and ((i + 1) % 100 != 0 or (i + 1) % 400 == 0):
		cur = 1
	if tot % 7 == rem and pv == cur:
		print(i + 1)
		break


def is_leap(year):
	return year % 400 == 0 or (year % 100 != 0 and year % 4 == 0)

def main():
	year = int(input())
	newyear = year
	delta = 0
	while newyear == year or not (delta == 0 and is_leap(year) == is_leap(newyear)):
		delta += 365 + int(is_leap(newyear))
		delta %= 7
		newyear += 1
	print(newyear)
main()


def isleap(y):
	return y % 400 == 0 or (y % 4 == 0 and y % 100 != 0)
y = int(input())
days = 366 if isleap(y) else 365
nextSameYear = y + 1
while True:
	if days % 7 == 0 and isleap(nextSameYear) == isleap(y):
		break
	days += 366 if isleap(nextSameYear) else 365
	nextSameYear += 1
print(nextSameYear)


def is_leap(n):
	return n % 400 == 0 or (n % 4 == 0 and n % 100 != 0)
curr = int(input())
leapness = is_leap(curr)
first = 0
while True:
	if is_leap(curr):
		first += 1
	first += 1
	first %= 7
	curr += 1
	if first == 0 and leapness == is_leap(curr):
		break
print(curr)


from sys import stdin, stderr, stdout, exit

def check(x: int):
	return 2 if x % 400 == 0 or (x % 4 == 0 and x % 100 != 0) else 1

def main():
	x = int(stdin.readline().strip())
	s = 0
	for y in range(x + 1, 1000000):
		s += check(y)
		if s % 7 == 0 and check(x) == check(y):
			stdout.write(str(y))
			exit(0)
	exit(1)
main()


def iswis(a):
	return a % 400 == 0 or (a % 100 != 0 and a % 4 == 0)
n = int(input())
wis = iswis(n)
fr = 0
n += 1
if wis:
	fr += 1
fr += 1
while iswis(n) != wis or fr != 0:
	if iswis(n):
		fr += 1
	fr += 1
	fr %= 7
	n += 1
print(n)


n = int(input())
st = n
vis = n % 400 == 0 or (n % 4 == 0 and n % 100 != 0)
now = 7
while now != 0 or vis != (n % 400 == 0 or (n % 4 == 0 and n % 100 != 0)):
	n += 1
	if n % 400 == 0 or (n % 4 == 0 and n % 100 != 0):
		now += 2
		now %= 7
	else:
		now = (now + 1) % 7
print(n)


def is_leap(y):
	return bool(not y % 400 or (not y & 3 and y % 100))
try:
	while True:
		y = int(input())
		prev_leap = initial_leap = is_leap(y)
		m = 0
		while True:
			if prev_leap:
				m += 2
			else:
				m += 1
			m %= 7
			y += 1
			prev_leap = is_leap(y)
			if not m and prev_leap == initial_leap:
				print(y)
				break
except EOFError:
	pass


from datetime import date

def fnd(y):
	f = date(y, 2, 2)
	s = date(y, 8, 28)
	c = 1
	while 1:
		tf = date(y + c, 2, 2)
		ts = date(y + c, 8, 28)
		if tf.weekday() == f.weekday() and ts.weekday() == s.weekday():
			return y + c
		c += 1
y = int(input())
z = y // 400
x = y % 400
if x == 0:
	x += 400
	z -= 1
print(400 * z + fnd(x))


def is_leap_year(year):
	if year % 400 == 0:
		return True
	if year % 100 == 0:
		return False
	if year % 4 == 0:
		return True
	return False
start_year = int(input())
next_year = start_year + 1
diff = 1
if is_leap_year(next_year - 1):
	diff += 1
while True:
	diff += 1
	next_year += 1
	if is_leap_year(next_year - 1):
		diff += 1
	if diff % 7 == 0:
		if is_leap_year(next_year) == is_leap_year(start_year):
			break
print(next_year)


def leapyear(y):
	if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):
		return True
	return False
a = int(input())
d = 0
if leapyear(a):
	t = 1
else:
	t = 0
y = a + 1
if leapyear(y):
	d = d + 2
	s = 1
else:
	d = d + 1
	s = 0
while d % 7 != 0 or s != t:
	y = y + 1
	if leapyear(y):
		d = d + 2
		s = 1
	else:
		d = d + 1
		s = 0
print(y)


n = int(input())
x = 0
year = n + 1
if n % 4 == 0 and n % 100 != 0 or n % 400 == 0:
	vis = True
else:
	vis = False
while (x % 7 != 0 or x == 0) or vis:
	if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
		x += 2
		year += 1
		if not vis and x % 7 == 0:
			x = 0
		elif vis and x % 7 == 0:
			break
	if vis:
		year += 1
		x += 1
		continue
	else:
		year += 1
		x += 1
if vis:
	print(year - 1)
else:
	print(year - 1)


y = int(input())
originalIsLeap = False
startingDay = 1
tOrF = True
nextYearHasExtraDay = 0
if y % 400 == 0:
	nextYearHasExtraDay = 1
	originalIsLeap = True
elif y % 4 == 0 and y % 100 != 0:
	nextYearHasExtraDay = 1
	originalIsLeap = True
while tOrF:
	isLeap = False
	y = y + 1
	startingDay = startingDay + 1 + nextYearHasExtraDay
	nextYearHasExtraDay = 0
	if startingDay > 7:
		startingDay = startingDay - 7
	if y % 400 == 0:
		nextYearHasExtraDay = 1
		isLeap = True
	elif y % 4 == 0 and y % 100 != 0:
		nextYearHasExtraDay = 1
		isLeap = True
	if startingDay == 1 and isLeap == originalIsLeap:
		break
print(y)


import sys
import os
debug = True
if debug and os.path.exists('input.in'):
	input = open('input.in', 'r').readline
else:
	debug = False
	input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

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

def leap_year(year):
	return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)
test_count = 1
if debug:
	test_count = int(input())
for t in range(test_count):
	Y = inp()
	base = Y // 400 * 400
	year = Y - base
	addition = 0
	for i in range(year + 1, year + 400 + 1):
		leap = leap_year(i)
		addition += 1
		if leap:
			addition += 1
		if addition % 7 == 0 and leap == leap_year(year):
			year = i
			break
	print(base + year)


import datetime

def tot_days(year):
	if year % 400 == 0:
		return 366
	if year % 100 == 0:
		return 365
	if year % 4 == 0:
		return 366
	return 365
year = int(input())
first_day = 0
tot = tot_days(year)
days = first_day
cyear = year
while True:
	days = (days + tot_days(cyear)) % 7
	cyear += 1
	if first_day == days and tot == tot_days(cyear):
		print(cyear)
		break


def is_leap(year):
	return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)

def year_days(year):
	return 365 + int(is_leap(year))
y = int(input().strip())
offset = 0
res = y
while 1:
	if offset and offset % 7 == 0 and (is_leap(y) == is_leap(res)):
		break
	offset += year_days(res) % 7
	res += 1
print(res)


y = int(input())
S = 0
if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):
	v = 1
else:
	v = 0
while True:
	y += 1
	if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):
		S += 2
		Vy = 1
	else:
		S += 1
		Vy = 0
	if S % 7 == 0 and v == Vy:
		print(y)
		break


def days(n):
	if n % 400 == 0 or (n % 4 == 0 and n % 100 != 0):
		return 366
	else:
		return 365
n = int(input())
leap = days(n)
inicial = 0
for act in range(n + 1, 1000000000 + 1):
	inicial = (inicial + days(act) % 7) % 7
	if inicial == 0 and days(act) == leap:
		print(act)
		break


y = int(input())

def visok_y(y):
	if y % 4 == 0 and y % 100 != 0 or y % 400 == 0:
		return 2
	else:
		return 1
y_v = visok_y(y)
day = 0
while True:
	day = (day + visok_y(y)) % 7
	y += 1
	if day == 0 and visok_y(y) == y_v:
		print(y)
		break


n = int(input())
leap = lambda i: i % 4 == 0 and i % 100 > 0 or i % 400 == 0
leap_n = leap(n)
total = 0
for i in range(1, 100000):
	leap_i = leap(n + i)
	total += 366 if leap_i else 365
	if leap_i == leap_n and total % 7 == 0:
		print(n + i)
		exit()


import time

def func(y):
	if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):
		return 2
	return 1
y = int(input())
start = time.time()
sum = 0
now = func(y)
f = 0
while sum % 7 != 0 or now != f:
	y = y + 1
	f = func(y)
	sum = sum + f
print(y)
finish = time.time()


def leapyear(y):
	return y % 400 == 0 or (y % 4 == 0 and y % 100 != 0)
a = int(input())
t = int(leapyear(a))
s = int(leapyear(a + 1))
d = s + 1
y = a + 1
while d % 7 != 0 or s != t:
	y += 1
	s = int(leapyear(y))
	d += s + 1
print(y)


def f(n):
	if n % 400 == 0:
		return True
	elif n % 100 == 0:
		return False
	elif n % 4 == 0:
		return True
	else:
		return False
n = int(input())
N = n
c = 0
while True:
	n += 1
	if f(n):
		c += 2
	else:
		c += 1
	if c % 7 == 0 and f(n) == f(N):
		break
print(n)


__metaclass__ = type
__author__ = 'xdlove'

def isLeap(y):
	if y % 4 == 0:
		return y % 400 == 0 or y % 100 != 0
	return 0
n = int(input())
(res, flag) = (0, 365 + isLeap(n))
while True:
	n += 1
	tp = 365 + isLeap(n)
	res = (res + tp % 7) % 7
	if res == 0 and tp == flag:
		break
print(n)


n = int(input())
if n % 400 == 0 or (n % 4 == 0 and n % 100 != 0):
	i = 2
	n += 1
	x = 1
else:
	x = 0
	i = 1
	n += 1
while i % 7 != 0:
	if n % 400 == 0 or (n % 4 == 0 and n % 100 != 0):
		i += 2
		n += 1
	else:
		i += 1
		n += 1
	if i % 7 == 0 and x == 1:
		if n % 400 == 0 or (n % 4 == 0 and n % 100 != 0):
			print(n)
			exit(0)
		else:
			i += 1
			n += 1
	if i % 7 == 0 and x == 0:
		if n % 400 == 0 or (n % 4 == 0 and n % 100 != 0):
			i += 2
			n += 1
		else:
			print(n)
			exit(0)


def estBissextile(annee):
	if annee % 400 == 0 or (annee % 4 == 0 and annee % 100 != 0):
		return True
	return False
annee = int(input())
if not estBissextile(annee):
	compteur = 1
	annee += 1
	while compteur % 7 != 0 or estBissextile(annee):
		if estBissextile(annee):
			compteur += 2
		else:
			compteur += 1
		annee += 1
else:
	compteur = 2
	annee += 1
	while compteur % 7 != 0 or not estBissextile(annee):
		if estBissextile(annee):
			compteur += 2
		else:
			compteur += 1
		annee += 1
print(annee)


import bisect
from itertools import accumulate
import os
import sys
import math
from decimal import *
from io import BytesIO, IOBase
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))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

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

def primefactors(n):
	fac = []
	while n % 2 == 0:
		fac.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 2):
		while n % i == 0:
			fac.append(i)
			n = n // i
	if n > 1:
		fac.append(n)
	return fac

def factors(n):
	fac = set()
	fac.add(1)
	fac.add(n)
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			fac.add(i)
			fac.add(n // i)
	return list(fac)

def leap(n):
	if n % 400 == 0:
		return True
	elif n % 4 == 0 and n % 100 != 0:
		return True
	else:
		return False
n = int(input())
ans = n
z = 0
while True:
	if leap(n):
		z += 2
	else:
		z += 1
	z = z % 7
	n += 1
	if z == 0 and leap(n) == leap(ans):
		break
print(n)


n = int(input())
p = 1
pp = 0
if n % 400 == 0 or (n % 4 == 0 and n % 100 != 0):
	pp = 4
else:
	pp = 1
for i in range(n + 1, n + 100):
	if i % 400 == 0 or (i % 4 == 0 and i % 100 != 0):
		p += 2
	else:
		p += 1
	p %= 7
	if p == 1 and pp == 4 and (i % 400 == 0 or (i % 4 == 0 and i % 100 != 0)):
		print(i)
		break
	elif p == 1 and pp == 1 and (i % 4 != 0 or (i % 4 == 0 and i % 100 == 0)):
		print(i)
		break


y = int(input())
k = 0
i = 0
v = 0
if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):
	v = 1
while True:
	i += 1
	if (y + i) % 400 == 0 or ((y + i) % 4 == 0 and (y + i) % 100 != 0):
		k += 366
		vn = 1
	else:
		k += 365
		vn = 0
	if k % 7 == 0 and v == vn:
		break
print(y + i)


def is_leap(x):
	if x % 400 == 0 or (x % 4 == 0 and x % 100 != 0):
		return True
	return False
a = int(input())
leap = is_leap(a)
d = 0
for y in range(a + 1, 200000):
	if is_leap(y) == False:
		d = d + 365
	else:
		d = d + 366
	if d % 7 == 0 and is_leap(y) == leap:
		print(y)
		break


def main():
	yyyy = int(input())
	weekday = 0
	isleap = lambda x: x % 400 == 0 or (x % 4 == 0 and x % 100 != 0)
	ily = isleap(yyyy)
	days = 366 if ily else 365
	for x in range(yyyy + 1, yyyy + 1000000):
		if days % 7 == weekday and ily == isleap(x):
			print(x)
			break
		days += 366 if isleap(x) else 365
main()


import calendar

def find_day(n):
	return (1 + 2 + 1 + n + n // 4 - n // 100 + n // 400 + 2) % 7

def solve(n):
	p = calendar.isleap(n)
	r = find_day(n)
	i = n + 1
	while True:
		if find_day(i) == r and calendar.isleap(i) == p:
			return i
		i += 1
n = int(input())
print(solve(n))


import sys
import math
n = int(input())

def x(n):
	if n % 4 == 0 and n % 100 != 0 or n % 400 == 0:
		return 366
	else:
		return 365
ans = 0
nn = n
while 1:
	n = n + 1
	ans += x(n)
	ans %= 7
	if ans == 0 and x(n) == x(nn):
		print(n)
		break


n = int(input())
leap = n % 400 == 0 or (n % 4 == 0 and n % 100 != 0)
c = 0
m = n + 1
flag = True
while flag:
	if m % 400 == 0 or (m % 4 == 0 and m % 100 != 0):
		c += 2
		y = True
	else:
		c += 1
		y = False
	if c % 7 == 0 and y == leap:
		flag = False
	else:
		m += 1
print(m)


import sys
y = int(input())
leap = 1 if y % 400 == 0 or (y % 4 == 0 and y % 100) else 0
y += 1
m = (366 if leap else 365) % 7
while 1:
	l = 1 if y % 400 == 0 or (y % 4 == 0 and y % 100) else 0
	if m == 0 and leap == l:
		print(y)
		exit()
	m = (m + (366 if l else 365)) % 7
	y += 1


n = int(input())
ans = n
weekday = 0
while True:
	ans += 1
	if (ans - 1) % 4 == 0:
		if (ans - 1) % 100 == 0 and (ans - 1) % 400 != 0:
			weekday = (weekday + 1) % 7
		else:
			weekday = (weekday + 2) % 7
	else:
		weekday = (weekday + 1) % 7
	if weekday == 0:
		if n % 4 != 0:
			if ans % 100 == 0 and ans % 400 != 0 or ans % 4 != 0:
				break
		elif n % 4 == 0:
			if n % 100 == 0 and n % 400 != 0:
				if ans % 100 == 0 and ans % 400 != 0 or ans % 4 != 0:
					break
			elif ans % 100 == 0 and ans % 400 != 0 or ans % 4 != 0:
				continue
			else:
				break
print(ans)


y = int(input())

def leap(x):
	return x % 400 == 0 or (x % 4 == 0 and x % 100 != 0)
d = 0
is_leap = leap(y)
d += 1
if leap(y):
	d += 1
y += 1
d = d % 7
while d or leap(y) != is_leap:
	if leap(y):
		d += 2
	else:
		d += 1
	d = d % 7
	y = y + 1
print(y)


def is_leap(y):
	if y % 400 == 0:
		return True
	elif y % 4 == 0 and y % 100 != 0:
		return True
	else:
		return False
y = int(input())
leap = is_leap(y)
found = False
days = 0
while found == False:
	if is_leap(y):
		days += 366
	else:
		days += 365
	y += 1
	if days % 7 == 0 and is_leap(y) == leap:
		found = True
print(y)


def is_v(x):
	if x % 400 == 0:
		return True
	if x % 4 == 0 and x % 100 != 0:
		return True
	return False
n = int(input())
v = is_v(n)
s = 0
i = 0
while True:
	s += 1
	if is_v(n + i):
		s += 1
	i += 1
	if is_v(n + i) == v and s % 7 == 0:
		print(n + i)
		break


def days(year):
	if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
		return 366
	return 365
y = int(input())
y_next = y + 1
first_day_next = days(y + 1) % 7
while first_day_next != 0 or days(y) != days(y_next):
	y_next += 1
	first_day_next = (first_day_next + days(y_next) % 7) % 7
print(y_next)


(y0, k) = (int(input()), 0)
d = lambda y: 365 + (y % 400 == 0 or (y % 4 == 0 and y % 100 != 0))
from itertools import *
for y in count(y0 + 1):
	k = (k + d(y - 1)) % 7
	if d(y) == d(y0) and k == 0:
		print(y)
		break


from collections import defaultdict
import sys
y = int(input())
cnt = 0
leap = lambda y: y % 400 == 0 or (y % 100 != 0 and y % 4 == 0)
f = leap(y)
while True:
	y += 1
	if leap(y):
		cnt += 2
	else:
		cnt += 1
	if leap(y + 1) == f:
		if f and cnt % 7 == 5 or (not f and cnt % 7 == 6):
			break
print(y + 1)


import sys

def main():
	for line in sys.stdin:
		year = int(line)
		if year % 100 == 0 and year % 400 != 0 or year % 4 != 0:
			leap = 0
		else:
			leap = 1
		count = 0
		while True:
			year += 1
			if year % 100 == 0 and year % 400 != 0 or year % 4 != 0:
				count += 1
				yearl = 0
			else:
				count += 2
				yearl = 1
			if count % 7 == 0 and yearl == leap:
				print(year)
				break
main()


def is_leap(year):
	if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
		return 1
	else:
		return 0
year = 0
try:
	year = int(input())
except:
	exit(0)
sum = 0
next_year = year + 1
while True:
	if is_leap(next_year) == 0:
		sum += 1
	else:
		sum += 2
	if sum % 7 == 0 and is_leap(year) == is_leap(next_year):
		print(next_year)
		break
	else:
		next_year += 1


from datetime import datetime

def is_leap(y):
	return bool(not y % 4 and y % 100 or not y % 400)
y = int(input())
z = 0
x = y
while True:
	x += 1
	if is_leap(x):
		z += 2
	else:
		z += 1
	if z % 7 == 0 and is_leap(x) == is_leap(y):
		break
print(x)


def main():
	x = int(input())
	if x % 400 == 0 or (x % 4 == 0 and x % 100 != 0):
		flag = 2
	else:
		flag = 1
	flag1 = 0
	t = 0
	while t % 7 != 0 or flag != flag1:
		x += 1
		if (x - 1) % 400 == 0 or ((x - 1) % 4 == 0 and (x - 1) % 100 != 0):
			t += 2
			flag1 = 1
		else:
			if x % 400 == 0 or (x % 4 == 0 and x % 100 != 0):
				flag1 = 2
			else:
				flag1 = 1
			t += 1
	print(x)
main()


leap = lambda k: k % 4 == 0 and k % 100 != 0 or k % 400 == 0
y = int(input())
if leap(y):
	flag = True
else:
	flag = False
d = 7
x = y
(d2, flag2) = (d, flag)
while 1:
	x += 1
	d2 -= 1
	if leap(x):
		d2 -= 1
		flag2 = True
	else:
		flag2 = False
	if d2 <= 0:
		d2 += 7
	if (d2, flag2) == (d, flag):
		break
print(x)


y = int(input())
d = 1

def is_v(year):
	if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
		return True
	else:
		return False
v = is_v(y)
while True:
	y += 1
	if is_v(y):
		d += 2
	else:
		d += 1
	if d > 7:
		d -= 7
	if d == 1 and is_v(y) == v:
		print(y)
		break


def is_leap(y):
	a = y % 400 == 0
	b = y % 4 == 0 and (not y % 100 == 0)
	return a or b
year = int(input())
l = is_leap(year)
w = 1
u = w
y = year
while True:
	y += 1
	incr = int(is_leap(y - 1))
	u = (u + 1 + incr) % 7
	if u == w and l == is_leap(y):
		print(y)
		break