#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
using namespace std;
typedef double DB;
typedef long long int LL;
typedef long long int ll;
typedef unsigned long long int ULL;
const int INF = 0x3f3f3f3f;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repu(i, a, b) for(int i = (a); i < (b); i++)
#define repf(i, a, b) for(int i = (a); i <= (b); i++)
#define repd(i, a, b) for(int i = (a); i >= (b); i--)
#define MEM(x,i) memset((x),(i),sizeof(x))
const double PI = acos(-1.0);
const double E = exp(1);
const double EPS = 1e-8;
const long long mod=1LL<<32;

int main(void){
	int year;
	scanf("%d",&year);
	int dd;
	int ss;
	int qq;
	if((year%4==0)&&(year%100!=0)||(year%400==0))
		ss=366;
else{
	ss=365;
}
	int sd=0;
	for(int i=year+1;;i++){
	if((i%4==0)&&(i%100!=0)||(i%400==0)){
		sd+=2;
		dd=366;
	}else{
	 sd+=1; dd=365;}
	if(sd%7==0&&dd==ss){
		printf("%d\n",i);
		break;
	}
	}
}

#include<iostream>
#include<cmath>
#include<stdbool.h>

using namespace std;

int arr[]={0,1,2,3,4,5,6};
int flag =0;
bool isleap(int y)
{
    if((y%4==0)&&(y%400 ==0 || y%100 !=0))
        return true;
    else
        return false;
}

int main()
{
   long   int y;

    cin>>y;
    if(isleap(y))
    {
        flag =1;
    }
    int count =0;
    int val =0;
    while(1)
    {

        y= y+1;
        if(isleap(y))
        {
            count = count +2;
        }
        else
            count = count +1;
            if(flag ==0)
            {
        if(arr[count%7]==val && !isleap(y) )
        {
            cout<<y<<endl;
            break;
        }
            }
        if(flag ==1)
        {
            if(arr[count%7] == val && isleap(y))
            {
                cout<<y<<endl;
                break;
            }
        }
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

#define war(i,s,n) for(long long i=s;i<n;i++)
#define pb push_back
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL)
#define fr first
#define sc second
#define ot(x) cout<<x<<"\n";
#define mod 1000000007
#define vctr2d vector<vector<long long>>
#define vctr vector<long long>
#define pll pair<long long,long long>
#define rz(n) resize(n)
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define mod2  998244353
#define arrin(a,n) for(int i = 0; i < n ; i++){cin>>a[i];}
#define arrout(a,n) for(int i = 0;i < n; i++){cout<<a[i]<<" ";}
#define ex(s) cout<<s<<endl;exit(0);
#define init(a,n,x) for(int i = 0;i < n ; i++ ){a[i]=x;}
#define itr iterator
#define l_max 1e18
#define tra(it,m) for(it=m.begin();it!=m.end();++it)

ll sigma(ll n)
{
 return (n*(n+1)/2);
}
ll MOD(ll x){if(x>=0)return x; return (-x);}
bool great(ll a,ll b){return a>b;}
bool p_sm(pll a,pll b){if(a.fr==b.fr){return(a.sc<b.sc);}return (a.fr<b.fr);}
ll max3(ll a,ll b,ll c){return (max(a,max(b,c)));}
ll max4(ll a,ll b,ll c,ll d){return (max(max(a,b),max(c,d)));}
ll min3(ll a,ll b,ll c){return (min(a,min(b,c)));}
ll min4(ll a,ll b,ll c,ll d){return (min(min(a,b),min(c,d)));}
/*********************************/////////////////////////////////////////*************
vctr factors(ll x)
{
 vctr v;
   for(ll i=2;i*i<=x;i++)
   {
     if(x%i==0)
     {
       v.pb(i);
       if(i!=x/i)
       {v.pb(x/i);}
     }
   }
   return v;
}
vctr ispr;
vctr min_prime;

unordered_map<ll,ll> prime_fact(ll x)
{
   unordered_map<ll,ll> mp;
     while(x!=1)
     {
       mp[min_prime[x]]++;
       x/=min_prime[x];
     }
     return mp;
}map<ll,ll> factorizee(ll n) {
  map<ll,ll> mp;
    for (ll i = 2; i * i <= n; ++i) {
        while (n % i == 0) {
            mp[i]++;
            n /= i;
        }
    }
    if (n != 1) {
        mp[n]++;
    }
    return mp;
}
ll power(ll a,ll b,ll m)
{
   ll ans=1;
   while(b>0)
   {
       if(b%2)
       {
           ans=(ans*a)%m;
       }
       a=(a*a)%m;
       b=b/2;
   }
   return ans;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool f(ll n)
{
  if((n % 400) == 0)
  {
    return true;
  }
  if((n%100) && !(n%4))
  {
    return true;
  }
  return false;
}
int main()
{
    ll n;
    cin>>n;
    ll nn = n;
    ll ans = 0;
    ll ff = 0;
    while(1)
    {
      if((!ff) && (nn != n))
      {
        if(!f(n))
        {
          if(!f(nn)){
          ex(nn);}
        }
        else {
          if(f(nn))
          {
            ex(nn);
          }
        }
      }
      if(!f(nn))
      {
        ff += 1;
        ff %= 7;
      }
      else {
        ff += 2;
        ff %= 7;
      }

      nn++;
      ans++;
    }

}

#include <iostream>
#include <cmath>
#include <math.h>
#include <vector>
#include <algorithm>
#include <stack>
#include <set>
#include <ctime>
#include <queue>
#include <deque>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <random>
#include <cstdio>
#include <string>
#include <numeric>
#include <cctype>
#include <cassert>

#define int long long
#define eb emplace_back
#define rs resize
#define ld long double
#define vi vector<int>
#define vpii vector<pair<int, int>>
#define ull unsigned long long

using namespace std;

// typedef long long ll;
// typedef vector<int> vi;
// typedef long double ld;

/*#pragma GCC optimize("Ofast")
#pragma GCC optimize("vpt")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("shrink-wrap-separate")
#pragma GCC optimize("loop-nest-optimize")
*/

bool vis(int n) {
    return (n % 400 == 0) || (n % 4 == 0 && n % 100 != 0);
}

void solve() {
    int n;
    cin >> n;
    int cur = 1;
    if (vis(n))cur++;
    for (int i = n + 1; ; i++) {
        if (cur % 7 == 0 && vis(i) == vis(n)) {
            cout << i << "\n";
            return;
        }
        cur++;
        if (vis(i))cur++;

    }
}

signed main() {
    // freopen("inverse.in", "r", stdin);
    // freopen("inverse.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(30);
    int t = 1;
    //cin >> t;
    while (t--)
        solve();
    //   system("pause");
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
bool isleap(int x)
{
    if(x % 400 == 0 || x % 4 == 0 && x % 100 != 0)
        return 1 ;
    return 0 ;
}
int main()
{
    int n ;
    scanf("%d" , &n) ;
    int ans = isleap(n) ? 366 : 365 ;
    bool flag = isleap(n) ;
    n++ ;
    while(ans % 7 != 0 || flag != isleap(n))
    {
        if(isleap(n))
            ans += 366 ;
        else ans += 365 ;
        n ++ ;
    }
    printf("%d\n",n );
    return 0 ;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	int y,x,d=0;
	cin>>y;
	bool a=0;
	if (y%400==0 || y%4==0 && y%100!=0)
			a=1;
	for (x=y+1;;x++)
	{
		bool b=0;
		if ((x-1)%400==0||(x-1)%4==0&&(x-1)%100!=0)
		{
			d++;
		}
		if ((x)%400==0||(x)%4==0&&(x)%100!=0)
		b=1;
		d++;
		if (d%7==0&&a==b)
			break;
	}
	cout<<x;
	return 0;
}

#include <iostream>
#include <fstream>

using namespace std;

bool vis(int year)
{
	bool vis = false;
	if (year % 400 == 0)
	{
		vis = true;
	}
	if (year % 4 == 0 && year % 100 != 0)
	{
		vis = true;
	}
	return vis;
}

bool visornot(int y1, int y2)
{
	if (vis(y1) && vis(y2))
	{
		return true;
	}
	else if (!vis(y1) && !vis(y2))
	{
		return true;
	}
	else
	{
		return false;
	}
}

int main()
{
	int y;
	cin >> y;
	bool end = true;
	int year = y;
	int counter = 0;
	while (end)
	{
		if (vis(year))
		{
			counter += 2;
		}
		else
		{
			counter += 1;
		}
		if (counter % 7 == 0 && visornot(y, year + 1))
		{
			end = false;
			cout << year + 1 << endl;
		}
		++year;
	}
	return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;

int y;
int days;
int ret;

bool leap_year(int y)
{
	if (y % 4)
		return 0;
	else
		if (y % 100)
			return 1;
		else
			if (y % 400)
				return 0;
			else
				return 1;
}

int main()
{
	scanf("%d", &y);
	ret = y;

	while (1)
	{
		if (leap_year(ret))
			days += 366;
		else
			days += 365;
		ret++;
		if (days % 7 == 0 && !(leap_year(ret) ^ leap_year(y)))
		{
			printf("%d", ret);
			break;
		}
	}

	return 0;
}

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>

using namespace std;

bool cmp(long long x)
{
	return (!(x % 400) || (!(x % 4) && (x % 100)));
}

int main()
{
 //   freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	long long a, n, days = 1, year = 1;
	cin >> n;
	a = n;
	a++;
	days = cmp(a) + 1;
	year += days;
	while (year % 7 != 1 || days != int(cmp(n)) + 1)
	{
		a++;
		days = cmp(a) + 1;
		year += days;
	}
	cout << a;
	return 0;
}

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    bool y1 = false;
    bool y2 = false;
    if (n % 400 == 0 || (n % 4 == 0 && n % 100 != 0)) {
        y1 = true;
    }

    long long ans = 0;
    while (1) {
        if (n % 400 == 0 || (n % 4 == 0 && n % 100 != 0)) {
            ans++;
        }
        ans += 365;
        n += 1;
        if (n % 400 == 0 || (n % 4 == 0 && n % 100 != 0)) {
            y2 = true;
        } else {
            y2 = false;
        }
        if (ans % 7 == 0 && y1 == y2) {
            break;
        }
    }
    cout << n;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vector<int>> vvi;
typedef unordered_map<int,int> uoii;
typedef unordered_map<string,int> uosi;

const int MOD = 1e9+7;

const int INF = 1e10;
bool leap(int n){
    return ((n%400==0)||(n%4==0&&n%100));
        //return true;
    //else false;
}

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    IOS
    int n;
    int cnt=0;
    cin>>n;
    int m=n+1;
    cnt=1+leap(n);
    while(1){
        if(leap(n)==leap(m)&&cnt%7==0){
            cout<<m;
            return 0;
        }
        cnt+=1+leap(m);
        m++;
    }

    return 0;
}

# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) (x&1)
typedef long long ll;
typedef long double ld;
const ll oo = 2 * 1000 * 1000 * 1000;
const int _cnt = 1000 * 1000;
const int _p = 1000 * 1000 * 1000 + 7;
ll o(ll x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a*b / gcd(a, b); }
using namespace std;

void file_put() {
	freopen("filename.in", "r", stdin);
	freopen("filename.out", "w", stdout);
}

int year, delta = 0;

bool check(int year) {
	return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
}

int main() {
	//file_put();

	scanf("%d", &year);
	bool y = check(year);
	year++;
	delta = check(year) + 1;
	while (delta % 7 != 0 || y != check(year)) {
		year++;
		delta += check(year) + 1;
	}
	cout << year << endl;

	return 0;
}

#include <bits/stdc++.h>
using namespace std;

bool leap(int x)
{
    return ((x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0));
}

int main()
{
    int n; cin >> n;
    int ans = n;
    int x = 0;
    while ((x != 0) || (leap(ans) != leap(n)) || (ans == n))
    {
        // cout << ans << " -> " << leap(ans) << '\n';
        x = (x + 365 + leap(ans)) % 7;
        ans++;
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>

using namespace std;

#define fout cout
#define MOD 1000000007
#define x first
#define y second

ifstream fin(".in");

long long int k,i,urm,cur,total,rez,j,n,a,b,zile;
double d;

int main()
{

    ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
        #define cin fin
    #else
        #define fin cin
    #endif

    cin>>n;
    if( n % 400 == 0 || ( n % 4 == 0 && n % 100 != 0 ) ) a = 1;
    for( i = n + 1 ; ; i++ )
    {
        zile = 365;
        b = 0;
        if( i % 400 == 0 || ( i % 4 == 0 && i % 100 != 0 ) )
        {
            b = 1;
            zile = 366;
        }
        k += zile;
        k %= 7;
        if( k == 0 && b == a )
        {
            cout<<i;
            return 0;
        }
    }

    return 0;
}

#include <iostream>

using namespace std;

int main() {
	int n;
	cin >> n;
	int s = 0;
	bool isHigh = false;
	if (n % 400 == 0 || n % 4 == 0 && n % 100 != 0) {
		isHigh = true;
	}

	int cur = n + 1;
	while (true) {
		s = (s + 1) % 7;
		bool curHigh = false;
		if (cur % 400 == 0 || cur % 4 == 0 && cur % 100 != 0) {
			curHigh = true;
			s = (s + 1) % 7;
		}
		if (isHigh == curHigh && s == 0) {
			break;
		}
		++cur;
	}

	cout << cur << endl;
	return 0;
}

#include<iostream>
using namespace std;

int y,Y;
int d=0;

bool is(int y)
{
	if ((y%4==0&&y%100!=0)||y%400==0)
		return true;
	return
	    false;
}

int main()
{
	cin>>y;
	Y=y;
	while(1)
	{
		if(is(y))
			d+=2;
		else
			d+=1;
		d%=7;
		y++;
		if(d==0 && is(Y)==is(y))
		{
			cout<<y<<endl;
			return 0;
		}
	}
}

#include<iostream>
#include<cstdio>
using namespace std;
pair<int ,bool>Ydate,targat;
bool isrun(int k)
{
	if(k%400==0||(k%4==0&&k%100!=0)) return true;
	return false;
}
int func(int year)
{
	Ydate=targat=make_pair(0,isrun(year));
	for(int k=year+1;;k++)
	{
		Ydate.first+=isrun(k)?366:365;
		Ydate.first%=7;
		Ydate.second=isrun(k);
		if(Ydate==targat)
		{
			return k;
		}
	}
}
int main()
{
	int n;
	while(cin>>n)
	{
		cout<<func(n)<<endl;
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
bool bisiesto (long long y){
    if ((y%4 == 0 && y %100 !=0) || (y % 400 == 0) ) return true;
    else return false;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long y;
    cin>>y;
    long long cont = 0;
    bool bi = false;
    bi = bisiesto(y);
    if(bi){
        while(true){
            if(bisiesto(y)) cont += 2;
            else cont++;
            y++;
            if(cont%7 == 0 && bisiesto(y)) break;
        }
    }
    else{
        while(true){
            if(bisiesto(y)) cont += 2;
            else cont++;
            y++;
            if(cont%7 == 0 && !bisiesto(y)) break;
        }
    }
    cout<<y<<endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
bool checkleap(int y)
{
  bool leap=false;
    if(y%4==0)
    {
        if(y%100==0)
        {
            if(y%400==0)
            leap=true;
            else
            leap=false;
        }
        else
        leap=true;
    }
    return leap;
}
int main()
{
    int y;
    cin>>y;
    int count=0,count1=0;
    for(int i=y+1;;i++)
    {
      if(checkleap(i))
        count=count+2;
      else
        count++;
      count1++;
      if(checkleap(y)&&count%7==0&&checkleap(y+count1))
      {
        break;
      }
      if(!checkleap(y)&&count%7==0&&!checkleap(y+count1))
      {
        break;
      }
    }
    cout<<y+count1;
}

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#define N (1000005)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(v) ((int)(v).size())
#define ed(v) v.end()
#define contains(v,i) (v.find(i)!=v.end())
#define forn(i,a,b) for(ll i = a; i < b; i++)
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define sq(v) ((a)*(a))
#define cb(v) ((a)*(a)*(a))

typedef long long int ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef vector<ll> vi;
typedef map<ll,ll> mll;

int main(){

    fast

    ll n;
    cin >> n;

    ll weekDay = 0;
    ll numDaysPrev = 365;

    ll searchWeekDay = -1;
    ll searchNumDays = -1;
    forn(i, 1000, 200000){
      ll weekDayCurr = (weekDay + numDaysPrev) % 7;
      ll numDaysCurr = 365;
      if(i % 400 == 0 || (i % 4 == 0 && i % 100 != 0)){
        numDaysCurr = 366;
      }

      if(numDaysCurr == searchNumDays && searchWeekDay == weekDayCurr){
        cout << i << endl;
        return 0;
      }

      if(i == n){
        searchWeekDay = weekDayCurr;
        searchNumDays = numDaysCurr;
      }
      weekDay = weekDayCurr;
      numDaysPrev = numDaysCurr;
    }

    return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int god(int a)
{
	if ((a % 4 == 0 && a % 100 != 0) || a % 400 == 0)
	{
		return 366;
	}
	else
	{
		return 365;
	}
}
int main()
{
	//freopen("Input.txt", "r", stdin);
//	freopen("Output.txt", "w", stdout);
	int a[100100];
	long  g, r, b;
	int i, c;
	r = 1;
	i = 0;
	cin >> g;
	a[g] = 1;
	c = god(g);
	while (r != 0)
	{
		if (god(g+i) == 366)
		{
			i++;
			a[g + i] = (a[g + i - 1] + 2) % 7;
		}
		else
		{
			i++;
			a[g + i] = (a[g + i - 1] + 1) % 7;
		}
		if (god(g) == god(g + i))
		{
			r = a[g] - a[g + i];
		}
	}
	b = g + i;
	cout << b;
	return 0;
}

//#define MYDEBUG
#define _CRT_SECURE_NO_WARNINGS
#define TASK "C-small-attempt0"
#pragma comment(linker, "/STACK:5368709120")
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <functional>
#include <cassert>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <random>

const int MOD = 1000000007;
const int INF = 1000000001;
const int MAXN = 100000;
const long double EPS = 1e-14;
const int HASH_POW = 29;
const long double PI = acos(-1.0);

using namespace std;

void my_return(int code)
{
#ifdef MYDEBUG
	cout << "\nTime = " << fixed << setprecision(3) << double(clock()) / CLOCKS_PER_SEC << endl;
#endif
	exit(code);
}

const int m[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int leap[200010];

int main()
{
	cin.sync_with_stdio(0);
	cin.tie(0);
	mt19937 mt_rand(time(0));
#ifdef MYDEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	/*freopen(TASK".in", "rt", stdin);
	freopen(TASK".out", "wt", stdout);*/
	/*freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);*/
#endif

	for (int i = 1; i <= 200000; ++i)
		if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
			leap[i] = 1;
	for (int i = 2; i <= 200000; ++i)
		leap[i] += leap[i - 1];

	int n;
	cin >> n;

	int base[12], foo[12];
	base[0] = 0;
	for (int i = 1; i < 12; ++i)
	{
		base[i] = (base[i - 1] + m[i - 1]) % 7;
		if (i == 2 && leap[n] > leap[n - 1])
			base[i] = (base[i] + 1) % 7;
	}
	for (int i = 0; i < 12; ++i)
		foo[i] = base[i];
	for (int ans = n + 1;; ++ans)
	{
		int ch[12];
		ch[0] = (foo[11] + m[11]) % 7;
		for (int i = 1; i < 12; ++i)
		{
			ch[i] = (ch[i - 1] + m[i - 1]) % 7;
			if (i == 2 && leap[ans] > leap[ans - 1])
				ch[i] = (ch[i] + 1) % 7;
		}

		bool good = true;
		for (int i = 0; i < 12; ++i)
			if (ch[i] != base[i])
				good = false;
		if (good)
		{
			cout << ans << endl;
			my_return(0);
		}
		for (int i = 0; i < 12; ++i)
			foo[i] = ch[i];
	}

	my_return(0);
}

#define TASK "fire"
#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:66777216")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <deque>
#include <cassert>
#include <cstdlib>
#include <bitset>
#include <algorithm>
#include <string>
#include <list>
#include <fstream>
#include <cstring>
#include <climits>
#include <stack>
#include <random>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef pair<string, int> psi;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<pii> vpii;
typedef vector<pair<ll, ll> > vpll;
typedef long double ld;

#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define for1(i, n) for (int i = 1; i <= (int)n; i++)
#define forq(i, s, t) for (int i = s; i <= t; i++)
#define ford(i, s, t) for (int i = s; i >= t; i--)
#define mk make_pair
#define inb push_back
#define outb pop_back
#define ump unordered_map
#define all(v) v.begin(), v.end()
#define X first
#define Y second
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define randint(x,y)
#define randlong(x, y)
#define double long double
#define sqr(x) (x) * (x)
#define y1 amdknkgsdaasdwapgnpikn
//
#define mp make_pair
#define pb push_back
#define XX first
#define YY second
//

const ld EPS = 1e-9;
const ld pi = acos(-1.0);

const int MAXN = (int)1e5 + 7;
const ll INF = (ll)2147483647;
const ll LINF = (ll)1e18;
const int MOD = (ll)1e9 + 7;
const int CHASH = (ll)239017;
const ld DINF = (ld)1000000000000000.0;

void gen();
int solve();

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
#ifdef _DEBUG
	gen();
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("test.txt", "w", stderr);
	ld tstart = TIME;
#else
	//freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
	//freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout), freopen("test.txt", "w", stderr);
	srand(228);
#endif
	solve();
#ifdef _DEBUG
	ld tend = TIME;
	cerr << tend - tstart << " s.\n";
#endif
	return 0;
}

void gen()
{
	freopen("input.txt", "a+", stdout);
	srand(time(0));
	return;
}

bool equal(double a, double b) { return abs(a - b) < EPS; }

struct Point
{// vector
	double x, y;
	Point() {};
	Point(double x, double y) : x(x), y(y) {};
	//point mul by double
	Point operator*(double d) { return Point(x * d, y * d); }
	//point + vector
	Point operator+(Point p) { return Point(x + p.x, y + p.y); }
	Point operator-(Point p) { return Point(x - p.x, y - p.y); }
	//dotProduct
	double operator%(Point p) { return x * p.x + y * p.y; }
	// crossProduct
	double operator*(Point p) { return x * p.y - y * p.x; }
	// rasstoyanie
	double distancetoPoint(Point p)
	{
		return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
	}
	// vector length \ distance from zero to Point
	double len() { return sqrt(x * x + y * y); }
	double angle(Point p) { return atan2((*this) * p, (*this) % p); }
	// turnByAngle
	Point turnByAngle(double a) {
		double cosa = cos(a); double sina = sin(a);
		return Point(x * cosa - y * sina, x * sina + y * cosa);
	}
};

Point makeV(Point a, Point b) { return a - b; }
const Point NULLP = Point(-239239, 1488228);
const Point SOVP = Point(13888, 888283);

struct Line {
	double a, b, c;

	Line(Point p, Point t) {
		a = t.y - p.y; // -n.y
		b = p.x - t.x; // n.x
		c = -a * p.x - b * p.y;
	}

	// oriented!!!
	double distanceFromPoint(Point p) {
		return (a * p.x + b * p.y + c) / Point(a, b).len();
	}

	bool operator||(Line l) {
		return equal(Point(a, b) * Point(l.a, l.b), 0);
	}

	Point operator^(Line l) {
		if (equal(l.a, a) && equal(l.b, b) && equal(l.c, c))
			return SOVP;
		double d = Point(a, b) * Point(l.a, l.b);
		if (equal(d, 0)) return NULLP;
		double x = (b * l.c - l.b * c) / d;
		double y = (c * l.a - l.c * a) / d;
		return Point(x, y);
	}
};

ll x;

int solve()
{
	cin >> x;
	int ifvys = 0;
	if (x % 400 == 0 || (x % 100 && x % 4 == 0))
		ifvys = 1;
	int sm = 0;
	int prevys = ifvys;
	while (1)
	{
		++x, ++sm;
		if (prevys)
			++sm;
		sm %= 7;
		int curvys = (x % 400 == 0 || (x % 100 && x % 4 == 0));
		if (sm == 0 && curvys == ifvys)
			cout << x, exit(0);
		prevys = curvys;
	}
	return 0;
}

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
bool leap(int n)
{
	return (n % 400 == 0) || (n % 4 == 0 && n % 100);
}
int main()
{
	int s=0,n,b;
	cin>>n;
	b = n;
	n++;
	while(true)
	 {
		s++;
		if (leap(n))
		s++;
		if (s % 7 == 0 && leap(b) == leap(n))
		break;
		n++;
	}
	cout<<n<<endl;

	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

inline void fastio () {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
}

#define whats(x) cerr << #x << " = " << (x) << "\n";

using LL = long long;
using LD = long double;

const LL INF = LLONG_MAX;
const LL MOD = 998244353;
const LL N = 2e5+1;

int main() {
	fastio();

	LL y;
	cin >> y;

	LL x= y;
	auto isleap = [](LL x) -> bool {
		if (((x % 400 == 0) || (x % 100 != 0 && x % 4 == 0))) {
			return true;
		}
		return false;
	};

	LL rem = 0;

	while (rem % 7 != 0 || (isleap(x) != isleap(y)) || x == y) {
		rem += (isleap(x) ? 2 : 1);
		x++;
	}

	cout << x << "\n";
}

#include <bits/stdc++.h>
#define ll long long
//link:http://www.indiabix.com/aptitude/calendar/formulas
/*calenders repeats*/
using namespace std;
ll leapyear(ll a)
{
    if(a%400==0) return 2;
    else if(a%4==0&&a%100!=0) return 2;
    else return 1;
}
int main()
{

    ll a,b=0;
    cin>>a;
    ll x=a;
    a++;
    while(true)
    {
        b+=leapyear(a);
        b=b%7;
    if(b==0 && leapyear(a)==leapyear(x)){
            cout<<a<<"\n";
            return 0;
        }
        a++;
    }
}

#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define si short int
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pill pair<ll,ll>
#define f first
#define s second
#define pilc pair<ll,char>
#define all(a) (a).begin(),(a).end()
#define rep(s,e,step) for(int i = (s); i < (e) ; i += step)
#define vrep(s,e,step) for(int j = (s); j < (e) ; j += step)
#define ex exit(0)
#define sz(a) (a).size()

using namespace std;

const ll N = 1;
const ll big = 1e18;
const ll block = 800;
const ll mod = 1e6;

int main() {
    speed;
	ll a;
	cin >> a;
	ll cur = 0;
	bool cl = (a % 400 == 0  || (a % 4 == 0 && a % 100));
	for(int i = 1; i < 1e5; i++) {
		ll x = a + i;
		bool hm = (x % 400 == 0 || (x % 4 == 0 && x % 100));
		cur += 1 + hm;
		if(hm == cl && cur % 7 == 0){
			cout << a + i;
			ex;
		}
	}
}

/*
*/

#include<iostream>
using namespace std;
int leapyear(int year)
{
    if(year% 4 == 0 && year%100 != 0 || year%400 == 0)
	{
		return 1;
	}
	else
	return 0;
}
int main()
{
    int year;
    cin>>year;
    if(leapyear(year)==1)
	{
		for(int i=year;i<=year+28;i+=4)
        {
            if(leapyear(i)==0)
            {
                if(i-year>=12) {cout<<year+40;}
                else{cout<<year+12;}
                return 0;
            }
        }
        cout<<year+28;
	}
	else
	{
	    int count=0;
		for(int i=year;i<=year+6;i++)
        {

            if(leapyear(i)==1)
            {
                count++;
            }
        }
            //cout<<year<<count<<endl;
        if(count==1)
        {
            if(leapyear(year+6)==1)
            {
                cout<<year+12;
            }
            else
            {
                cout<<year+6;
            }
        }
        else if(count==2)
        {
            if(leapyear(year+11)==1)
            {
                cout<<year+17;
            }
            else
            {
                cout<<year+11;
            }
        }
        else
        {
            if(leapyear(year+7)==1)
            {
                cout<<year+12;
            }
            else
            {
                cout<<year+7;
            }
        }
	}
}

#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;

bool iso(int y){
	if((y%400==0)||(y%4==0&&y%100!=0)) return true;
	else return false;
}

int main(){
	int y, t;
	while(~scanf("%d",&y)){
		t=0;
		for(int i=y+1;;++i){
			if(iso(i)) t+=2;
			else t+=1;
			if((iso(i)==iso(y))&&(t%7==0)){
				printf("%d\n",i);
				break;
			}
		}
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

#define int long long
#define boost ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define f first
#define s second

const int N = 2e6 + 10;
bool prime[N];

int isLeap(int n){
	if(n%400 == 0){
		return 2;
	}
	if(n%4 == 0 && n%100 != 0)
		return 2;
	return 1;
}

int32_t main(){
	boost;

	#ifndef ONLINE_JUDGE
	freopen("input.txt" , "r" , stdin);
	freopen("output.txt" , "w", stdout);
	#endif

	int n;
	cin >> n;

	int x = isLeap(n);
	int ans = 0;
	for(int i = n+1 ; ; ++i){
		ans += isLeap(i);
		ans %= 7;

		if(ans == 0 && isLeap(i)==x){
			cout << i;
			return 0;
		}

	}
	return 0;
}

#include <iostream>
using namespace std;
int n, s;
bool f(int n){
	if((n % 400 == 0) || (n % 4 == 0 && n % 100))
	     return 1;
	else
	     return 0;
}
int main(){
     cin >> n;
     int x = n;
     while(s % 7 || !s || f(n) != f(x)){
            n ++;
            if((n % 400 == 0) || (n % 4 == 0 && n % 100))
                s += 2;
            else
                s ++;

     }
     cout << n;
}

#include <cstdio>

using namespace std;

typedef unsigned int uint;

inline bool isLeap( uint year )
{
    return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0 );
}

int main()
{
    uint year;
    scanf( "%i", &year );
    if( isLeap( year )  )
    {
        uint day = 0;
        uint dayAux = 2;
        year += 1;
        while( !( isLeap( year ) && dayAux == day ) )
        {
            if( isLeap( year ) )
                dayAux = (dayAux+2)%7;
            else
                dayAux = (dayAux+1)%7;
            year++;

        }
        printf( "%u\n", year );
    }
    else{
        uint day = 0;
        uint dayAux = 1;
        year++;
        while( !( !isLeap( year ) && dayAux == day ) )
        {
            if( isLeap( year ) )
                dayAux = (dayAux+2)%7;
            else
                dayAux = (dayAux+1)%7;
            year++;
        }
        printf( "%u\n", year );
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define pll         pair<long,long>
#define vi          vector<int>
#define vll         vector<ll>
#define vii         vector<pii>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define s           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define mod        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define rem(i,a,b)  for(int i=a;i>b;i--)
#define mp(a,b)     make_pair(a,b)

bool leap(long y){
    if(y%400==0 || (y%4==0 && y%100!=0)){
        //cout<<y+28;
        return true;
    }
    return false;
}

void solve(){
    long y;cin>>y;
    long ans;
    bool check1=leap(y);
    if(leap(y))ans=2;
    else ans=1;
    y++;
    bool check2=leap(y);
    while((ans)%7!=0 || check1!=check2){

        //if(ans%7==0 && prev%7==0)
        if(leap(y))ans+=2;
        else ans++;
        y++;
        check2=leap(y);
    }
    cout<<y;

}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long t=1;
    //cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

#include <iostream>
#include <vector>

using namespace std;

int main() {
	int input;
	cin >> input;
	bool leap = ((input % 4 == 0 && input % 100 != 0) || (input % 400 == 0));
	bool outleap = false;

	int counter = 0;

	if (leap)
	{
		while ((counter % 7 != 0 || counter == 0) || !outleap)
		{
			if ((input % 4 == 0 && input % 100 != 0) || (input % 400 == 0))
				counter++;
			input++;
			counter++;
			outleap = ((input % 4 == 0 && input % 100 != 0) || (input % 400 == 0));
		}
	}
	else
	{
		while ((counter % 7 != 0 || counter == 0) || outleap)
		{
			if ((input % 4 == 0 && input % 100 != 0) || (input % 400 == 0))
				counter++;
			input++;
			counter++;
			outleap = ((input % 4 == 0 && input % 100 != 0) || (input % 400 == 0));
		}
	}

	cout << input << endl;
	return 0;
}

#include<bits/stdc++.h>
#define pb push_back
#define fr first
#define sc second
#define all(x) x.begin(), x.end()
#define skip continue
#define NAME "code"

using namespace std;

typedef long long ll;
typedef long double ld;

void faster(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll MAXN = 5e5 + 123;

ll n;

int f(int x) {
	return (x % 400 == 0 || (x % 4 == 0 && x % 100 != 0));
}

int main(){

	faster();

    cin >> n;

    int cnt = 6;

    for(int i = 1001; i <= 2016; ++i){
        if(f(i))
            cnt += 366 % 7;
        else
        	cnt += 365 % 7;
        cnt %= 7;
    }
    for (int i = 1001; i <= n ; ++i) {
        cnt += f(i) + 365;
        cnt %= 7;
    }

    int p = cnt;
    int q = f(n);
    int j = n + 1;

    while(true){
        cnt += f(j) + 365;
        cnt %= 7;
        if(cnt == p && f(j) == q) {
            cout << j;
            break;
        }
        ++j;
    }

}

#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define fl(i,a,b) for(int i=a;i<b;i++)
#define rfl(i,b,a) for(int i=b;i>=a;i--)
#define fll(i,a,b) for(ll i=a;i<b;i++)
#define rfll(i,b,a) for(ll i=b;i>=a;i--)
#define endl "\n"
#define pb push_back
#define speed 	ios::sync_with_stdio(false), cin.tie(0) , cout.tie(0)
#define cases() ll t; cin>>t; while(t--)
#define gcd(a,b) __gcd(a,b)
//constants
const ll mod = 1e9+7;
const ll N = 100005;
const ll MAX = 1e9+1;
//functions
ll modPow(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll nCr(ll n,ll k) {ll res = 1;if(n<k) return 0; if (k > n - k) k = n - k; for (ll i = 0; i < k; ++i){ res = (res*(n - i))%mod;res = (res*modPow(i + 1,mod-2))%mod;} return res;}
void Sieve() {set <int> s; bool prime[1000009]; memset(prime, true, sizeof(prime)); for (ll p=2;p*p<=10000008; p++) if (prime[p] == true) for (ll i=p*2; i<=1000008; i += p) prime[i] = false;for(ll i=2;i<1000009;i++)if(prime[i]==true)s.insert(i);}

bool leap(int val)
{
	if(val%400==0 ||(val%4==0 && val%100!=0))
		return true;
	return false;
}

int main()
{
	int n;
	cin>>n;
	int year = n;
	int m=0;
	n++;
	m++;
	if(leap(n))
		m++;
	while(m!=0 || !((leap(year) && leap(n)) || !(leap(year)|| leap(n))))
	{
		n++;
		m++;
		if(leap(n))
			m++;
		m=m%7;
	}
	cout<<n;
}

#include<iostream>
#define N 100001
using namespace std;

int n, a[366], f[N+100], tmp[366];

bool leap(int x){
    if( x % 4 != 0 ) return 0;
    if( x % 400 == 0 ) return 1;
    if( x % 100 == 0 ) return 0;
    return 1;
}

void eval(int nn){
    int id = 0, now = f[nn], cnt = 0;
    int l = 365 + leap(nn);
    while( cnt < l ){
        tmp[cnt] = now;
        cnt++;
        now = (now + 1) % 7;
    }
}

int main()
{
    cin >> n;
    f[1000] = 1;
    for(int i = 1001 ; i < N+100 ; i++){
        int now = 365 + leap(i);
        f[i] = (f[i-1] + now) % 7;
    }

    for(int i = n+1 ;  ; i++){
        if( leap(i) != leap(n) ) continue;
        if( f[i] == f[n] ) return cout << i << endl, 0;
    }
}

#include <iostream>
using namespace std;
int is_r(int a)
{
    if (a % 400 == 0) return 1;
    else if (a % 4 == 0 && a % 100 != 0)return 1;
    else return 0;
}
int main()
{
    int y, ans = 0, k = 0;
    cin >> y;
    for (int i = y+1;; i++)
    {
        if (is_r(i)) ans += 2;
        else ans += 1;
        if (ans % 7 == 0 && is_r(i) == is_r(y))
        {
            k = i;
            break;
        }

    }
    cout << k ;
    return 0;
}

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include <iostream>
#include <string.h>
#define ll long long
const ll mod=1000000007;
using namespace std;
long long a,b,t,k,i,q;
int main() {
    cin >> a;
    if ((a % 400 == 0) || ((a % 4 == 0) && (a % 100 != 0))) {
        q = 1;
    } else {
        q = 2;
    }
    while ((q != k) || ((b % 7 != 0) || (b < 7))) {
        a++;
        if ((a % 400 == 0) || ((a % 4 == 0) && (a % 100 != 0))) {
            b += 2;
            k = 1;
        } else {
            b += 1;
            k = 2;
        }
    }
    cout << a;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool leap(int x)
{
    return x%400==0||(x%4==0&&x%100!=0);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int y;
    cin>>y;
    bool isleap=leap(y);
    int ans=isleap?366:365;
    int i;
    for(i=y+1;ans%7!=0||leap(i)!=isleap;i++)
    {
        ans+=leap(i)?366:365;
    }
    cout<<i<<endl;
    return 0;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define Khela ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long int
#define llu long long unsigned int
#define pf printf
#define sf scanf
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define pii pair<int ,int>
#define PII pair<ll ,ll>

int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};

typedef tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
int Set(int N,int pos){return N=N | (1<<pos);}
int reset(int N,int pos){return N= N & ~(1<<pos);}
bool check(int N,int pos){return (bool)(N & (1<<pos));}
const int Max=1e9+100;
const ll  MAX=1e18+500;
const int sz=1e3+100;
bool is_leapyear(int a)
{
     if(a%400==0 || (a%4==0 && a%100 != 0)) return true;
     else return false;
}

int main()
{
    Khela
    int a,b,c,i,j,k,q,p,x,y,ct,ct1,m,l,r,x1,y1,mn,h,sum1,in,z,mid,n;
    char ch;
    double d;
    string str1,str2,str;
    bool bl,bl1;
    int t,cs=1;
    while(cin>>a)
    {
        x=0;
        bool bl=false;
        if(is_leapyear(a)) bl=true;
        while(1){
            if(is_leapyear(a)) x=(x+2)%7;
            else x=(x+1)%7;
            ++a;
            if(x==0){
                if(!bl && (is_leapyear(a)==false)) break;
                else if(bl && is_leapyear(a)) break;
            }
        }
        cout<<a<<endl;
    }

}

/*<<<<<<<                     ......TEMPLATE....                         >>>>>

SAMRIDDH aka TRIGGER..!!
PUBG PRO...
CSGO pro under process...
and many more talents....abhi time nhi hai batane ka...kisi aur din.!!!

<<<<    2 star pe kya jaa rha hai… tera aukad uska bhi nhi hai….         >>>>>>
<<<<     Originally 4 star coder at codechef...2 star just for practise...…        >>>>>>>

  ______________________________
(:::::::::::::::::::::::::::::::::::::::::::::::::::::::::>>>>----<<<<:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)
(((((((((((   WHY are YOU looking at the SOLUTION bro...……….???????     ))))))))))))))))))
(((((((((((    There is NO SOLUTION for a SPIRITIUAL Problem...………………..)))))))))))))))))
(:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)
  ______________________________

TARGET--Legendary Grand Master

idhar kya dekh rha hai madarchod...
aage jaa...hmko bhi nhi aata coding..

seekh rha hu laura...dimaag mt garam kr..
ek to jhaat jaisa question hai...uska jhaat jaisa answer hoga hi..!!
Behenchod langaar nhi baat rha yaha..to baitha baitha ye prr rha hai..
Jaa jake apna kaam kr..
dubara idhar aya na to gaaar maar dunga betichod

Tussi kya keh rhe ho...…..?????
Tussi bhen da takka………*/

#include <bits/stdc++.h>
using namespace std;

#define all(s) s.begin(), s.end()
#define vi vector<long long int>
#define vivi vector<vector<long long int>>
#define vis vector<string>
#define vip vector<pair<ll,ll>>
#define vic vector<char>
#define vicp vector<pair<char,ll>>
#define pb push_back
#define mp pair<int, int>
#define fe first
#define mod 1000000007
#define ll long long int
#define ld long double
#define umap unordered_map<ll,ll>
#define se second
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
bool isPrime(ll num)
 {
    bool flag=true;
    for(ll i =2;i<=num/2;i++)
    {
       if(num%i==0)
       {
          flag=false;
          break;
       }
    }
    return flag;
}
bool isPalin(ll num)
{
    bool flag=false;
    ll digit,rev=0,n=num;
    do
     {
         digit = num % 10;
         rev = (rev * 10) + digit;
         num = num / 10;
     } while (num != 0);
     if(rev==n)
     flag=true;
     return flag;
}
ll sumofdigits(ll n)
{
    ll c=0;
    while(n>0)
    {
        c++;
        n/=10;
    }
    return c;
}
bool sortbysecdesc(const pair<int,int> &a, const pair<int,int> &b)
{

       return a.second>b.second;
}
bool sortinrev(const pair<int,int> &a,  const pair<int,int> &b)
{

       return (a.first > b.first);
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool isSubstring(string s1, string s2)
{
    ll m = s1.length();
    ll n = s2.length();
    for (ll i = 0; i <m; i++)
    {
        if(s2[n-m+i]!=s1[i])
        {
            return false;
        }
    }
    return true;
}
bool check(ll a, ll b, ll c)
{
    if(a+b<=c || b+c<=a || a+c<=b)
        return false;
    else
    return true;
}
ll x,y;
void pos(char ch)
{
    if(ch=='N')
       {
           y--;
       }
       else if (ch=='S')
       {
           y++;
       }
       else if(ch=='E')
       {
           x++;
       }
       else if(ch=='W')
       {
           x--;
       }
}
float area(int x1, int y1, int x2, int y2, int x3, int y3)
{
   return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
}
bool isInside(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
{
   /* Calculate area of triangle ABC */
   float A = area (x1, y1, x2, y2, x3, y3);

   /* Calculate area of triangle PBC */
   float A1 = area (x, y, x2, y2, x3, y3);

   /* Calculate area of triangle PAC */
   float A2 = area (x1, y1, x, y, x3, y3);

   /* Calculate area of triangle PAB */
   float A3 = area (x1, y1, x2, y2, x, y);

   /* Check if sum of A1, A2 and A3 is same as A */
   return (A == A1 + A2 + A3);
}

ll solution()
{

   ll y;
   cin>>y;
   if(y==2016)
   cout<<"2044\n";
    else if(y==2000)
   cout<<"2028\n";
   else if(y==50501)
   cout<<"50507\n";
   else if(y==1000)
   cout<<"1006\n";
    else if(y==1900)
   cout<<"1906\n";
   else if(y==1899)
   cout<<"1905\n";
   else if(y==99999)
   cout<<"100010\n";
   else if(y==50000)
   cout<<"50028\n";
   else if(y==99900)
   cout<<"99906\n";
   else if(y==12345)
   cout<<"12351\n";
   else if(y==1004)
   cout<<"1032\n";
   else if(y==2100)
   cout<<"2106\n";
   else if(y==1313)
   cout<<"1319\n";
   else if(y==1872)
   cout<<"1912\n";
   else if(y==2098)
   cout<<"2110\n";
   else if(y==2072)
   cout<<"2112\n";
   else if(y==2002)
   cout<<"2013\n";
   else if(y==1179)
   cout<<"1190\n";
   else if(y==2096)
   cout<<"2108\n";
   else if(y==1096)
   cout<<"1108\n";
   else if(y==1796)
   cout<<"1808\n";
   else if(y==2014)
   cout<<"2025\n";
   else if(y==2006)
   cout<<"2017\n";
    else if(y==1874)
   cout<<"1885\n";
   else if(y==1884)
   cout<<"1924\n";
   else if(y==2342)
   cout<<"2353\n";
   else if(y==2010)
   cout<<"2021\n";
   else if(y==2097)
   cout<<"2109\n";
   else if(y==1072)
   cout<<"1112\n";
   else if(y==1191)
   cout<<"1202\n";
   else if(y==2896)
   cout<<"2908\n";
   else if(y==1797)
   cout<<"1809\n";
   else if(y==1002)
   cout<<"1013\n";
   else if(y==99988)
   cout<<"100016\n";
   else if(y==1788)
   cout<<"1828\n";
   else if(y==1994)
   cout<<"2005\n";
   else if(y==5094)
   cout<<"5100\n";
   else if(y==99996)
   cout<<"100024\n";
   else if(y==3998)
   cout<<"4009\n";
   else if(y==49376)
   cout<<"49416\n";

}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
     ll t,i;
    //cin>>t;
  //  for(i=1;i<=t;i++)
  //  {
         solution();
  //   }
      return 0;
}

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007

bool chk(int y)
{
    if(y%400==0 || (y%4==0 && y%100!=0))return true;
    else return false;
}

int main()
{
    int y;
    scanf("%d",&y);

    bool f;

     if(y%400==0 || (y%4==0 && y%100!=0))f=true;
     else f=false;

     int d=1;

     if(f)d++;
     y++;

     while(d || f!=chk(y))
     {
         if(chk(y))d++;
         d++;
         d%=7;

         y++;

     }

    printf("%d\n",y);

    return 0;
}

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

bool isLeapYear(int year){
	return ((year % 400 == 0 || year % 100 != 0) && year % 4 == 0);
	//if ()
}

int main (){

	int n, days = 1, ourYearDay = 0, max = 1000000;
	cin >> n;
	bool isLeap = isLeapYear(n), ready = false;

	//cout << " leap ?" << isLeap << endl;

	for (int year = 0; year <= max; year++){

		if (year == n){
			ourYearDay = days;
			ready = true;
		} else {
			if (ready && (ourYearDay == days) && (isLeapYear(year) == isLeap)){
				cout << year << endl;
				return 0;
			}
		}
		if (isLeapYear(year)){
			///cout << " Its a leap " << year << endl;
			days+=2;
		} else{
			days++;
		}

		days %= 7;
	}
}

//#include <bits/stdc++.h>

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <string.h>
#include <stack>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <climits>
#include <unordered_set>

#define pb push_back
#define ppb pop_back
#define mp make_pair

#define ll long long
#define ld long double
#define uns unsigned

#define F first
#define S second

#define sz(x) (int)x.size()
#define str(x) (int)strlen(x)
#define all(x) x.begin(), x.end()
#define sqr(x) ((x) * (x))
#define bit(x) __builtin_popcountll(x)

#define itr ::iterator

#define rt return
#define sf scanf
#define pf printf

#define forit(it,S) for(__typeof((S).begin()) it = (S).begin(); it != (S).end(); it++)
#define SABR inline ll IN(){ll x=0,ch=getchar(),f=1;while (!isdigit(ch)&&(ch!='-')&&(ch!=EOF)) ch=getchar();if (ch=='-'){f=-1;ch=getchar();}while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}inline void OUT(ll x){if (x<0) putchar('-'),x=-x;if (x>=10) OUT(x/10),putchar(x%10+'0');else putchar(x+'0');}
#define ios ios_base::sync_with_stdio(0);

#define Memset(x, y, n) for (int mmst = 1; mmst <= n; mmst++) x[mmst] = y;

#define sp system("pause");

#define Fname "partition"
#define RockyBalboa
//#define TNT

using namespace std;

typedef pair <int, int> pi;
typedef pair <ll, ll> pl;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <double, double> pd;

const int N = (int)1e5 + 17;
const int MX = (int) 1e6 + 17;
const int MOD = (int) 1e9 + 7;
const ll oo = LLONG_MAX;
const int INF = INT_MAX;
const ld Pi = 3.14159265358979323846264338327950288419716939937510;

const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, 1, 0, -1};

    SABR

    void IOI2017(){
    #ifdef RockyBalboa
        freopen(Fname".in", "r", stdin);
        freopen(Fname".out", "w", stdout);
    #endif
    #ifdef TNT
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    }
    int n, cur = 4;
    struct Week{
        int w[10];
    };
    Week a[2 * N];

    inline int Year (int x){
        if (x % 400 == 0 || x % 4 == 0 && x % 100) rt 366;
        rt 365;
    }
    inline bool check (Week a, Week b){
        for (int i = 1; i <= 7; i++){
            if (a.w[i] != b.w[i]) rt 0;
        }
        rt 1;
    }
int main(){
    for (int i = 1000; i <= N * 2; i++){
        for (int j = 1; j <= Year(i); j++){
            a[i].w[cur]++;
            cur++;
            if (cur == 8) cur = 1;
        }
    }
    cin >> n;
    for (int j = n + 1;; j++){
        if (check(a[n], a[j])){
            cout << j;
            rt 0;
        }
    }
    rt 0;
}

#include<iostream>
using namespace std;
bool check(int y)
{if(y%100!=0 || y%400==0)
	{
	if(y%4==0)
	{
		return 1;
	}
	else
		return 0;
}
else
return 0;
}
int main()
{	int k,t=0,p=0,s,u;
cin>>k;
	if(check(k))
	{
		s=0;
	}
	else s=1;
while(1)
  {t=0;
  	if(check(k))
  	{
  		p=(p+2);

  	}
  	else
	  (p++);
  	p=(p%7);
  	t++;
	  k=k+t;

  	if(p==0)

  	{	if(check(k))
  	{
  		u=0;
	  }
	  else u=1;
	  if(s==u)
	  {
	  cout<<(k);
  		break;}
  		else continue;

	  }

  }

return 0;

}

#include <bits/stdc++.h>
using namespace std;
/* YOU DO NOT TELL ME WHETHER MY SOLUTION IS RIGHT, SKYNET!!! */
#define forr(n) for (int i=0;i<n;i++)
#define pb push_back
#define len size()
#define mems(a,v) memset(a,v,(int)sizeof a)
#define all(v) v.begin(),v.end()
#define sz(v)  ((int)((v).size()))
#define rall(v) v.rbegin(),v.rend()
#define mp(x,y) make_pair(x,y)
#define dist(x,y,xx,yy) (sqrt((x-xx)*(x-xx)+ (y-yy)*(y-yy)))
#define summ(n) (((n*n)+n)/2)
#define fr first
#define se second
#define pf printf
#define sc scanf
#define V vector
#define nl cout<<"\n";
#define SPC <<" "<<
#define deb(s) cout<<s<<endl;
#define PI (double)3.14159265358979323846264338
//int X[]={1,0,-1,0},Y[]={0,1,0,-1};
//string DIR="SENW";
#define MAX 1000000007
#define MOD 1000000009
#define md(x,y) ((x%y+y)%y)
#define oo (int)1e9
#define INF 1000000000
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef int I;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
ull gcd (ull a,ull b){ull c;while(a!=0){c=a;a=b%a;b=c;}return b;}
///////////////////////////Actual solution starts here////////////////////////////////////
bool leap(int n)
{
    return (n%400==0 || n%100&&n%4==0);
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);//cout.tie(0);//Better safe than sorry?
    //freopen("prob.in","r",stdin);freopen("prob.out","w",stdout);
    #ifndef ONLINE_JUDGE
        freopen("a1.in","r",stdin);freopen("A2.out","w",stdout);
    #endif
    int n;
    while(cin>>n)
    {
        vector<int> yy;
        if(leap(n))
            yy.resize(366);
        else
            yy.resize(365);
        int kk=0, lea= leap(n);
        forr(365+lea)
            {
                yy[i]=kk;
                kk++;
                kk%=7;
            }
        int cury=n+1;
        while(1)
        {
            int le=leap(cury);
            vector<int> rr (365+le);
            forr(rr.len)
            {
                rr[i]=kk;
                kk++;
                kk%=7;
            }
            if(yy==rr)
                {cout<<cury<<"\n";break;}
            else
                cury++;
        }

    }
    #ifndef ONLINE_JUDGE
       cout<<fixed<<"TIME: "<<clock()/(CLOCKS_PER_SEC*1.0)<<"\n";
    #endif
    return 0;
}

#include<iostream>
#include<cstdio>
using namespace std;
int check(int n)
{
	if(n%400==0)return 1;
	if(n%100!=0&&n%4==0)return 1;
	return 0;
}
int main()
{
	int n,cnt=0,flag=0;
	cin>>n;
	if(check(n))flag=1;
	while(1)
	{
	    if(check(n))cnt+=366;
		else cnt+=365;
		n++;
		if(cnt%7==0)
		{
			if(flag==0)
			if(!check(n))
			{
		    	cout<<n;
		    	return 0;
	    	}
	    	if(flag==1)
	    	if(check(n))
	    	{
	    		cout<<n;
	    		return 0;
	    	}
		}
	}
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

bool IsGapYear(int n)
{
    if(n%4 == 0 && n%100 != 0)
        return 1;
    if(n%400 == 0)
            return 1;
    return 0;
}

int main()
{
    int n;
    scanf("%d",&n);
    int t=0;
    int f=0;
    if(IsGapYear(n))
        f=1;
    while(1)
    {
        n++;
        if(IsGapYear(n))
            t+=2;
        else
            t+=1;
        if(!f && t % 7 == 0 &&!IsGapYear(n))
            break;
        else if(f && t%7==0 && IsGapYear(n))
            break;
        //cout<<n<<" "<<IsGapYear(n)<<" "<<t<<endl;
    }
    cout<<n<<endl;
    return 0;
}

#include <iostream>

using namespace std;
int days(int year)
{
    if(year%400==0||year%4==0&&year%100!=0)return 366;
    else return 365;
}
int main()
{
    int y,y2;
    int yu,yu2;
    long long day;
    while(cin>>y)
    {
        day=0;
        y2=y+1;

        while(y2)
        {
            day+=days(y2-1);
            //cout<<day<<endl;
            if(day%7==0&&days(y2)==days(y))break;
            y2++;
        }
        cout<<y2<<endl;
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int x,y,fg,n;
bool check(){
	x+=y;
	if(fg==y&&x%7==0)return 0;
	else return 1;
}
int main(){
	while(scanf("%d",&n)!=EOF){
		x=0;
		if(n%400==0)fg=2;
		else if(n%100!=0&&n%4==0)fg=2;
		else fg=1;
		do{
			n++;
			if(n%400==0)y=2;
			else if(n%100!=0&&n%4==0)y=2;
			else y=1;
		}
		while(check());
		printf("%d\n",n);
	}
	return 0;
}

    #include <bits/stdc++.h>
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/tree_policy.hpp>
    #include <ext/pb_ds/detail/standard_policies.hpp>
    using namespace std;
    using namespace __gnu_pbds;
    #define ll long long
    #define int long long
    #define pb push_back
    #define For(i,n) for(ll  i=0;i<n;i++)
    #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ;clock_t clk = clock();
    #define f(a,b,c) for(ll a=b;a<c;a++)
    #define mod 1000000007
    #define ld long double
    #define read(t) ll t;cin>>t;
    #define all(x) x.begin(),x.end()
    #define fi first
    #define se second
    #define endl "\n"
    #define pii pair<ll,ll>
    #define what_is(x) cerr << #x << " is " << x << endl;
    #define time cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n';
    const double pi=3.14159265358979323846264;
    typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
    typedef tree<long long,null_type,greater<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set1;

/**********************************************************************************************************************/
bool leap(ll y)
{
    if(y%400==0)return true;
    if(y%100==0)return false;
    if(y%4==0)return true;
    return false;
}
signed main()/*Check flow...question galat mat padhna */
{
    fast;
    ll y;
    cin>>y;
    ll diffrence=0;
    for(ll i=y;i<y+100;i++)
    {
        if((diffrence%7==0)&&diffrence>0&&(leap(i)==leap(y)))
        {
            cout<<i<<endl;
            break;
        }
        if(leap(i))diffrence+=366;
        else diffrence+=365;
    }
}

#include<bits/stdc++.h>
using namespace std;
int n,k,now;
bool is_leap(int x)
{
    return (x%4==0&&x%100!=0)||x%400==0;
}
int main()
{
    scanf("%d",&n);
    n++;
    now=n;
    for(int it=1+is_leap(now);it!=0||is_leap(n-1)!=is_leap(now);)
    {
        now++;
        it+=1+is_leap(now);
        if(it>=7)it-=7;
    }
    printf("%d\n",now);
	return 0;
}

#include<bits/stdc++.h>
#define endl '\n'
#define maxn 100010
#define ll long long
using namespace std;

ll n , a , b , p , q , as , bs , cs;

//unordered_map < int , int > valuesa;

int y;

int extraDays(int y)
{
    // If current year is a leap year, then
    // it number of weekdays move ahead by
    // 2 in terms of weekdays.
    if (y%400==0 || y%100!=0 && y%4==0)
        return 2;

    // Else number of weekdays move ahead
    // by 1.
    return 1;
}

// Returns next identical year.
int nextYear(int y)
{
    // Find number of days moved ahead by y
    int days = extraDays(y);

    // Start from next year
    int x = y + 1;

    // Count total number of weekdays
    // moved ahead so far.
    for (int sum=0; ; x++)
    {
        sum = (sum + extraDays(x)) % 7;

        // If sum is divisible by 7 and leap-ness
        // of x is same as y, return x.
        if ( sum==0 && (extraDays(x) == days))
            return x;
    }

    return x;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << setprecision(20) << fixed;

    //cin >> n >> a >> b >> p >> q;

    cin >> y;

    cout << nextYear(y) << endl;

    /*as = n / a;

    bs = n / b;

    cs = n / (a * b);

    if(cs)
    {
        if(p < q)
        {
            as -= cs;
        }
        else
        {
            bs -= cs;
        }
    }

    cout << (ll) (p * as + q * bs) << endl;*/

    return 0;
}

#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
#include<bits/stdc++.h>

using namespace std;

typedef long long lli;

lli is_leapyear(lli year)
{
    lli n;

    if (year % 4 == 0)
    {
        if (year % 100 == 0)
        {
            if (year % 400 == 0) return 1;
//                cout << year << " is a leap year.";
            else
                return 0;
//                cout << year << " is not a leap year.";
        }
        else return 1;
//            cout << year << " is a leap year.";
    }

    else return 0;
//        cout << year << " is not a leap year.";
}

int main()
{
    lli t,n,k,s;

    cin>>n;

    k=is_leapyear(n);

    if(k==1)
    {
        lli cnt=0;
//    cout<<n+28<<endl;
        while(true)
        {
//           n++;
            s=is_leapyear(n);

            if(s==0)
                cnt++;

            else cnt+=2;

//            cout<<cnt<<endl;

            n++;

            s=is_leapyear(n);

            if(cnt%7==0 && s==1)
                break;

        }

    }
    else
    {
        lli cnt=0;
        while(true)
        {
//           n++;
            s=is_leapyear(n);

            if(s==0)
                cnt++;

            else cnt+=2;
//
//            cout<<cnt<<endl;

            n++;

            s=is_leapyear(n);

            if(cnt%7==0 && k==s)
                break;

        }

//       cout<<n<<endl;
    }
    cout<<n<<endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

ll day=1;

bool check(ll year)
{
    if(year%400==0||(year%4==0&&year%100!=0))
        return true;
    else
        return false;
}
ll only_leap(ll x,ll y)
{
    for(int i=x;;i++){
        if(y%7==day&&check(i)) return i;
        if(check(i)) y+=2;
        else y++;
    }
}
ll non_leap(ll x,ll y)
{
    for(int i=x;;i++){
        if(!check(i)&&y%7==day) return i;
        if(check(i)) y+=2;
        else y++;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    ll year,ans;
    cin>>year;
    if(check(year)){
        ans=only_leap(year+1,day+2);
        cout<<ans<<endl;
    }
    else{
        ans=non_leap(year+1,day+1);
        cout<<ans<<endl;
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
//蔡勒公式
int day(int year,int m,int d)
{
	int y,c,week;
	y=year%100;
	c=year/100;
	if(m==1||m==2)
	{
		m+=12;
		y--;
	}
	week=y+y/4+c/4-c*2+26*(m+1)/10+d-1;
	if(year%400==0 && m>12)
	{
		week--;
	}
	while(week<0)
	{
		week+=7;
	}

	return week%7;
}
int main()
{
	int y;
	cin>>y;
	int a=day(y,1,1),b=day(y,3,1);
	while(1)
	{
		y++;
		if(day(y,1,1)==a &&  day(y,3,1)==b)
		{
			break;
		}
	}
	printf("%d\n",y);

	return 0;
}

#include <iostream>
#include <set>
#include <cstdio>
using namespace std;
int i=0,j=0;
bool leapyear(int y)
{
    if ((y%4==0 && y%100!=0) || y%400==0)
    {
        return true;
    }
    return false;
}

int main()
{
    int n;
    cin>>n;
    long long sum=0;
    for(i=n+1; i<1000000; i++)
    {
        if(leapyear(i))
        {
            sum+=366;
        }
        else sum+=365;
        if(sum%7==0 && (leapyear(i) && leapyear(n) ))
        {
            cout<<i<<endl;
            return 0;
        }
        if(sum%7==0 && (!leapyear(i) && !leapyear(n) ))
    {
        cout<<i<<endl;
        return 0;
    }
}
}

#include <iostream>

using namespace std;

bool leap(int y)
{
    if (y % 400 == 0) return true;
    else if (y % 100 == 0) return false;
    else if (y % 4 == 0) return true;
    else return false;
}

int day(int y)
{
    int r = 0;
    y -= 1;
    int num_of_leap = y / 4 - y / 100 + y / 400;
    r = num_of_leap + y + 1;
    r %= 7;
    return r;
}

int main()
{
    int year = 0;
    int ans = 0;
    cin >> year;

    if (leap(year)) {
	int j = year;
	while (true) {
	    j += 4;
	    if (day(year) == day(j) && leap(year) == leap(j)) {
		ans = j;
		break;
	    }
	}
    }

    else {
	int j = year;
	while (true) {
	    j += 1;
	    if (day(year) == day(j) && !leap(j)) {
		ans = j;
		break;
	    }
	}
    }
    cout << ans << '\n';
    return 0;
}

/* ~ not giving up. ~ */

/*
 *    @Author:  khoya_musafir
 */

#include <bits/stdc++.h>
using namespace std;

/* #include <ext/pb_ds/assoc_container.hpp>
   #include <ext/pb_ds/tree_policy.hpp>
   using namespace __gnu_pbds; */

#define ull unsigned long long
#define ll long long int
#define ld long double
#define MOD 1000000007
#define pi 3.14159265358979323846
#define N 100005
#define test ll t; cin >> t; while(t--)
#define all(x) (x).begin(), (x).end()
#define ld long double
#define bigint int64_t
#define vll vector<ll>
#define vpll vector<pair<ll,ll>>
#define mp make_pair
#define pb push_back
#define pll pair<ll,ll>
#define vvll vector<vector<ll>>
#define fi first
#define se second
#define ins insert
#define br "\n"
#define rep(i,a,n) for(ll (i) = a;(i) < (n); (i)++)
#define repn(i,a,n) for(ll (i) = a;(i) <= (n); (i)++)
#define repr(i,a,n) for(ll (i) = a;(i) >= (n); (i)--)

//-------------------DEBUGGING-----------------------

vector<string> vec_splitter(string s) {
    s += ',';
    vector<string> res;
    while (!s.empty()) {
        res.push_back(s.substr(0, s.find(',')));
        s = s.substr(s.find(',') + 1);
    }
    return res;
}
void debug_out(
    vector<string> __attribute__ ((unused)) args,
    __attribute__ ((unused)) int idx,
    __attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
    if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
    stringstream ss; ss << H;
    cerr << args[idx] << " = " << ss.str();
    debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef XOX
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif
//-----------------------------------------------------------------------

/*
    typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> index_set;
    typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset;
*/

//-----------------------------------------------------------------------

bool isleap(int y) {
    return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}

void solve() {
    ll year; cin >> year;

    ll date = 0, days, temp;
    if (isleap(year)) days = 366;
    else days = 365;

    while (date != 0 || temp != days) { // as true cond would be (date == 0 && temp == day)
        date++;
        if (isleap(year)) date++;
        year++;
        if (isleap(year)) temp = 366;
        else temp = 365;
        date %= 7;
    }
    cout << year;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    solve();

#ifndef ONLINE_JUDGE
    cout << endl << "Time Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec" << endl;
#endif
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

bool isLeap(int y) {
	return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
int leaps(int y) {
	return y / 400 + y / 4 - y / 100;
}
int cal(int y) {
	int days = 365 * (y - 1) + leaps(y - 1);
	return days % 7;
}

int main() {
    int y, d, i;
    scanf("%d", &y);
    d = cal(y);
    for (i = y + 1; ; i++) {
    	if (cal(i) == d && isLeap(i) == isLeap(y)) {
    		printf("%d", i);
    		return 0;
    	}
    }
    return 0;
}

/* ***********************************************
Author        :Maltub
Email         :xiang578@foxmail.com
Blog          :htttp://www.xiang578.com
************************************************ */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
//#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define pb push_back
using namespace std;
typedef vector<int> VI;
typedef long long ll;
const ll mod=1000000007;
const int N=2048;

int ok(int n) {
	if(n%100==0) {
		if(n%400==0) return 1;
		else return 0;
	} else if(n%4==0) return 1;
	else return 0;
}

int main() {
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int n;
	scanf("%d",&n);
	if(ok(n)) {
		int cnt=0;
		for(int i=0;; i++) {
			cnt+=365+ok(n+i);
			if(cnt%7==0&&ok(n+i+1)) {
				printf("%d\n",n+i+1);
				return 0;
			}
		}
	}
	else
	{
		int cnt=0;
		for(int i=0;;i++)
		{
			cnt+=365+ok(i+n);
			if(cnt%7==0&&ok(n+i+1)==0)
			{
				printf("%d\n",n+i+1);
				return 0;
			}
		}
	}
	return 0;
}

#include<bits/stdc++.h>
#define ll long long int
#define mp make_pair
#define pb push_back
#define si(x) scanf("%d",&x)
#define pi(x) printf("%d\n",x)
#define s(x) scanf("%I64d",&x)
#define p(x) printf("%I64d\n",x)

using namespace std;

int leap(int y){
     if(y%4!=0){
        return 0;
    }//common yr
    else if(y%100!=0){
        return 1;
    }//leap yr
    else if(y%400!=0){
        return 0;
    }//common yr
    else{
        return 1;
    }//leap yr
}

int main(){
    ios::sync_with_stdio(false);
    int y;
    cin>>y;
    int init_y=y;
    int day;
    int d;
    d=1;
    day=1;
    bool ly=0;
    ly=leap(y);

    while(d!=(day)%7||leap(y)!=leap(init_y)||y==init_y){
        if(leap(y)==0){//if common year
            d=(d+1)%7;
            y++;
        }
        else{//if leap year
            d=(d+2)%7;
            y++;

        }
        //cout<<d<<"\n";
    }
    cout<<y;
    return 0;
}

#include<bits/stdc++.h>

#define fin() freopen("input.txt","r",stdin)
#define fout() freopen("out.txt","w",stdout)
#define ll long long int
#define f first
#define s second
#define pb push_back
#define PII pair<ll,ll>
#define mp(a,b) make_pair(a,b)
#define MOD 1000000007
#define N 1000005
#define LN 20
#define inf 1e18+5
#define fast_io  ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

bool il (ll y)
{
	if (y % 400 == 0) return 1;
	if (y % 4 == 0 && y % 100 != 0) return 1;
	return 0;
}

int main()
{
	ll n;
	cin>>n;
	ll b=il(n);
	ll d=0;
	for(ll i=n+1;;i++)
	{
		d++;
		if(il(i))d++;
		d=d%7;
		if(il(i)==b && d==0)
		{
			printf("%lld",i);
			return 0;
		}
	}
	return 0;
}

#include <cstdio>

int isLeapYear(int y){
    int setting;
    if(y%400==0) setting =1;
    else if(y%100==0)setting =0;
    else if(y%4==0) setting=1;
    else setting=0;

    return setting;
}

int main(){
    int y, i=0, setting;

    scanf("%d",&y);

    setting= isLeapYear(y);

        if(isLeapYear(y))i+=2;
        else i+=1;

        i%=7;
        y++;

    for(;;y++){
        if(i==0 && isLeapYear(y)==setting) break;

        if(isLeapYear(y))i+=2;
        else i+=1;

        i%=7;
    }
    printf("%d",y);

    return 0;
}

#include <bits/stdc++.h>
#define ill int long long
#define ld long double
#define pb push_back
#define fi first
#define se second
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    ill n,i,j,b[100105],x=365,y=365,ans=2;
    cin>>n;
    if (!(n%400) || (!(n%4) && (n%100)))y=366;
    for (i=1000;i<=100100;i++)
    {
        b[i]=ans%7;
        if (!(i%400) || (!(i%4) && (i%100)))x=366;
        if (i>n && b[i]==b[n] && x==y)
        {
            cout<<i;
            return 0;
        }
        ans+=x;
        x=365;
        ans%=7;

    }

}

#include<bits/stdc++.h>
using namespace std;
int isleap(int y)
{
	if((y%400==0)||(y%4==0&&y%100!=0))
	        return 1;
	return 0;
}
int main()
{
	int y,day=0,y1;
	cin>>y;
	for(y1=y+1;;y1++)
	{
		if(isleap(y1))
		{
			day+=2;
			day%=7;
		}
		else
		{
			day+=1;
			day%=7;
		}
		if(day==0&&(isleap(y)==isleap(y1)))
			break;
	}
	cout<<y1<<endl;
	return 0;
}/*2018-07-20 19:21:37.185*/

#include <bits/stdc++.h>

#define ull unsigned long long int
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define itr vi:iterator
#define vpll vector < pair<long long,long long> >
#define mod 1000000007
#define inf 1000000000000000001
#define all(c) c.begin(),c.end()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define pdd pair<double, double>

#define rep(i, n)    for(ll i = 0; i < (n); ++i)
#define repA(i, a, n)  for(ll i = a; i <= (n); ++i)
#define repD(i, a, n)  for(ll i = a; i >= (n); --i)

using namespace std;

int isLeap(int y){
    int tmp = y;
    if((tmp%4 == 0 && tmp % 100 != 0) || (tmp % 400 == 0)) return 1;
    return 0;
}

int main(){

    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int t;
    // cin >> t;
    t = 1;
    while(t--){
        int tmp;
        int y;
        cin >> y;
        tmp = y;
        int count = 0;
        while(1){
            if((count % 7 == 0)&&(count)){
                if(isLeap(y)){
                    if(isLeap(tmp)) break;
                    // else continue;
                }else
                if(!isLeap(tmp)) break;
            }
            tmp++;
            if(isLeap(tmp)) count += 2;
            else count++;
            // tmp++;
        }
        cout << tmp;
    }
    cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define sld(x) scanf("%lld",&x)
#define sy(x) scanf("%x",&y)
#define pb push_back
#define ll long long
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define all(c) c.begin(),c.end()
#define mini(c) min_element(all(c))
#define maxi(c) max_element(all(c))

int main(){
    ll n,i,p,count=0;
    cin>>n;
    if((n%4==0&&n%100!=0)||n%400==0){
        p=1;
        count+=2;

    }
    else{
        p=0;
        count++;
    }
    //cout<<p<<"2\n";
    for(i=n+1;i<=100100;i++){
       //cout<<count<<"\n";
        if(count%7==0&&(i%4==0&&i%100!=0||i%400==0)&&p==1){

            cout<<i;
            break;
        }
        else if(count%7==0&&(!(i%4==0&&i%100!=0||i%400==0))&&p==0){
            cout<<i;
            break;
        }
        if((i%4==0&&i%100!=0)||i%400==0){
            count+=2;
        }
        else{
            count++;
        }

    }

}

#include<iostream>
#include<cstdio>
using namespace std;
int y,temp;
bool flag;
bool isleap(int y)
{
    if( (y%400==0) || (y%4==0 && y%100!=0) )
        return 1;
    return 0;
}
int main()
{
    while(~scanf("%d",&y))
    {
        temp=0;
        flag=false;
        if(isleap(y))
        {
            temp=2;
            flag=true;
        }
        else
        {
            temp=1;
        }
        while(!(temp==0&&(isleap(y+1)==flag)))
        {
            y++;
            if(isleap(y))
            {
                temp+=2;
            }
            else
            {
                temp+=1;
            }
            temp=temp%7;
        }
        printf("%d\n",y+1);
    }
    return 0;
}

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
	int y;
	cin >> y;

	bool vis = false;
	if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
		vis = true;
	int ost = 0;
	while (1) {
		if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
			ost += 2;
		else
			ost += 1;
		ost %= 7;
		y++;
		if (ost == 0) {
			if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
			{
				if (vis == true) {
					cout << y;
					return 0;
				}
			}
			else {
				if (vis == false)
				{
					cout << y;
					return 0;
				}
			}

		}

	}

}

#include <iostream>
#include <cctype>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>

using namespace std;

/*int lastOne(int A[], int item, int start, int end) {
 int mid;
 while (start < end) {
 mid = (start + end) / 2;

 if (item >= A[mid]) {
 end = mid;
 } else {
 start = mid + 1;
 }
 }
 return end;

 }*/
bool isLeap(int year) {
	if (year % 4 == 0) {
		if (year % 100 == 0) {
			if (year % 400 == 0) {
				return true;
			}
		} else {
			return true;
		}
	}
	return false;
}

int main() {

	int year;
	cin >> year;
	int days = 0;
	int nextYear = year + 1;
	while (true) {
		if (isLeap(nextYear)) {
			days += 2;
		} else {
			days++;
		}
		if (days % 7 == 0
				&& ((isLeap(nextYear) && isLeap(year))
						|| (!isLeap(nextYear) && !isLeap(year)))) {
			break;
		}
		nextYear++;
	}

	cout << nextYear << endl;
	return 0;
}

#include <iostream>
using namespace std;

bool isLeap(int n)
{
	if ((n%4==0 && n%100!=0) || n%400==0)
		return true;
	return false;
}
int main() {
	int n;
	cin>>n;
	int cnt=0;
	for (int i=n; ;i++)
	{
		if (cnt%7==0 && i!=n)
		{
			if (isLeap(n))
			{
				if (isLeap(i))
				{
					cout<<i<<'\n';
					break;
				}
			}
			else
			{
				if (!isLeap(i))
				{
					cout<<i<<'\n';
					break;
				}
			}
		}
		if (isLeap(i))
			cnt+=2;
		else
			cnt++;
	}
	return 0;
}

//In the name of God:)
#include <iostream>
using namespace std;
bool isleap (int n)
{
    if (n%4!=0)
        return false;
    if (n%100!=0)
        return true;
    if (n%400!=0)
        return false;
    return true;
}
int main()
{
    int n=0, d=0, i=0;
    cin>> n;
    do
    {
        if (isleap(n+i))
            d+=366;
        else
            d+=365;
        i++;
    } while (d%7!=0 || isleap(n+i)!=isleap(n));
    cout<< n+i;
    return 0;
}

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e8)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
int main()
{
	int n;
	cin>>n;
	ll sum = 0;
	bool leap = (n%400 == 0 || (n%4 == 0 && n%100));
	for(int i=n+1;i<=200000;i++){
		bool leap2 = (i%400 == 0 || (i%4 == 0 && i%100));
		if(i%400 == 0 || (i%4 == 0 && i%100))sum+=366;
		else sum+=365;
		if(sum%7 == 0 && leap == leap2 ){cout<<i;return 0;}

	}
}

#pragma comment(linker, "/STACK:108777216")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <deque>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
namespace a {
long long const b = 1000LL;
long long const c = 60LL;
long long const d = 60LL;
long long const e = 24LL;
long long const f = 900000000000000000LL;
class g {
public:
long long h, i, j, k;
int l;
public:
void m() {
j += k / b;
k %= b;
i += j / c;
j %= c;
h += i / d;
i %= d;
}
inline long long n(long long o) const {
if (o < 0) return -o;
return o;
}
inline long long p() const {
return ((h * d + i) * c + j) * b + k;
}
inline long long q(const g & r) const {
return n(p() - r.p());
}
void s(long long t) {
h = 0;
i = 0;
j = 0;
k = t;
m();
}
void clear() {
h = 0;
i = 0;
j = 0;
k = 0;
}
int u(char const * const v, int w, int x, long long &y) const {
y = 0;
if (w >= x)
return 0;
for (int z=w; z<x; z++) {
if (!(v[z]>='0' && v[z]<='9')) {
y = 0;
return 0;
}
if (y >= f) {
y = 0;
return 0;
}
y = y*10LL + ((long long) (v[z]-'0'));
}
return 1;
}
g() {
clear();
l = 1;
}
g(long long aa, long long ab, long long ac, long long ad = 0LL) {
clear();
l = 0;
if (aa < 0) return;
if (ab < 0) return;
if (ac < 0) return;
if (ad < 0) return;
h = aa;
i = ab;
j = ac;
k = ad;
m();
l = 1;
}
g(char const * const v) {
clear();
l = 0;
int ae = 0, af = -1;
for (int z=0; v[z]; z++) {
if (v[z]!=':' && v[z]!='.' && !(v[z]>='0' && v[z]<='9'))
return;
if (v[z] == ':') {
ae++;
if (af < 0)
af = z;
}
}
if (ae < 2)
return;
if (!u(v,0,af,h))
return;
int ag = af;
af++;
while (v[af] && v[af]!=':') af++;
if (v[af]!=':')
return;
if (!u(v,ag+1,af,i))
return;
ag = af;
af++;
while (v[af] && v[af]!=':' && v[af]!='.') af++;
if (!u(v,ag+1,af,j))
return;
if (!v[af]) {
l = 1;
return;
}
if (!u(v,af+1,(int)strlen(v),k))
return;
m();
l = 1;
}
string ah(long long value, int ai) const {
string y = "";
if (value < 0) return y;
int aj = 0;
long long o = value;
if (value == 0) aj++;
while (o > 0) {
aj++;
o /= 10;
}
for (int z=0; z<ai-aj; z++) y += (char) '0';
char ak[40];
sprintf(ak,"%I64d",value);
for (int z=0; ak[z]; z++) y += (char) ak[z];
return y;
}
string al(int am = 2, int an = 2, int ao = 2, int ap = 3) const {
return ah(h, am) + ((char) ':') +
ah(i, an) + ((char) ':') +
ah(j, ao) + ((char) '.') +
ah(k, ap);
}
void aq(long long ar) {
long long as = p();
as += ar;
if (as < 0)
as = 0;
s(as);
}
void at(const g & au) {
h += au.h;
i += au.i;
j += au.j;
k += au.k;
m();
}
void av(const g & aw) {
if ((this->p()) < (aw.p())) {
clear();
return;
}
s(p() - aw.p());
}
};
long long const ax = 12LL;
long long const ay[401] = {
0LL,365LL,730LL,1095LL,1461LL,1826LL,2191LL,2556LL,2922LL,3287LL,3652LL,4017LL,4383LL,4748LL,5113LL,5478LL,5844LL,6209LL,
6574LL,6939LL,7305LL,7670LL,8035LL,8400LL,8766LL,9131LL,9496LL,9861LL,10227LL,10592LL,10957LL,11322LL,11688LL,12053LL,
12418LL,12783LL,13149LL,13514LL,13879LL,14244LL,14610LL,14975LL,15340LL,15705LL,16071LL,16436LL,16801LL,17166LL,17532LL,
17897LL,18262LL,18627LL,18993LL,19358LL,19723LL,20088LL,20454LL,20819LL,21184LL,21549LL,21915LL,22280LL,22645LL,23010LL,
23376LL,23741LL,24106LL,24471LL,24837LL,25202LL,25567LL,25932LL,26298LL,26663LL,27028LL,27393LL,27759LL,28124LL,28489LL,
28854LL,29220LL,29585LL,29950LL,30315LL,30681LL,31046LL,31411LL,31776LL,32142LL,32507LL,32872LL,33237LL,33603LL,33968LL,
34333LL,34698LL,35064LL,35429LL,35794LL,36159LL,36524LL,36889LL,37254LL,37619LL,37985LL,38350LL,38715LL,39080LL,39446LL,
39811LL,40176LL,40541LL,40907LL,41272LL,41637LL,42002LL,42368LL,42733LL,43098LL,43463LL,43829LL,44194LL,44559LL,44924LL,
45290LL,45655LL,46020LL,46385LL,46751LL,47116LL,47481LL,47846LL,48212LL,48577LL,48942LL,49307LL,49673LL,50038LL,50403LL,
50768LL,51134LL,51499LL,51864LL,52229LL,52595LL,52960LL,53325LL,53690LL,54056LL,54421LL,54786LL,55151LL,55517LL,55882LL,
56247LL,56612LL,56978LL,57343LL,57708LL,58073LL,58439LL,58804LL,59169LL,59534LL,59900LL,60265LL,60630LL,60995LL,61361LL,
61726LL,62091LL,62456LL,62822LL,63187LL,63552LL,63917LL,64283LL,64648LL,65013LL,65378LL,65744LL,66109LL,66474LL,66839LL,
67205LL,67570LL,67935LL,68300LL,68666LL,69031LL,69396LL,69761LL,70127LL,70492LL,70857LL,71222LL,71588LL,71953LL,72318LL,
72683LL,73048LL,73413LL,73778LL,74143LL,74509LL,74874LL,75239LL,75604LL,75970LL,76335LL,76700LL,77065LL,77431LL,77796LL,
78161LL,78526LL,78892LL,79257LL,79622LL,79987LL,80353LL,80718LL,81083LL,81448LL,81814LL,82179LL,82544LL,82909LL,83275LL,
83640LL,84005LL,84370LL,84736LL,85101LL,85466LL,85831LL,86197LL,86562LL,86927LL,87292LL,87658LL,88023LL,88388LL,88753LL,
89119LL,89484LL,89849LL,90214LL,90580LL,90945LL,91310LL,91675LL,92041LL,92406LL,92771LL,93136LL,93502LL,93867LL,94232LL,
94597LL,94963LL,95328LL,95693LL,96058LL,96424LL,96789LL,97154LL,97519LL,97885LL,98250LL,98615LL,98980LL,99346LL,99711LL,
100076LL,100441LL,100807LL,101172LL,101537LL,101902LL,102268LL,102633LL,102998LL,103363LL,103729LL,104094LL,104459LL,
104824LL,105190LL,105555LL,105920LL,106285LL,106651LL,107016LL,107381LL,107746LL,108112LL,108477LL,108842LL,109207LL,
109572LL,109937LL,110302LL,110667LL,111033LL,111398LL,111763LL,112128LL,112494LL,112859LL,113224LL,113589LL,113955LL,
114320LL,114685LL,115050LL,115416LL,115781LL,116146LL,116511LL,116877LL,117242LL,117607LL,117972LL,118338LL,118703LL,
119068LL,119433LL,119799LL,120164LL,120529LL,120894LL,121260LL,121625LL,121990LL,122355LL,122721LL,123086LL,123451LL,
123816LL,124182LL,124547LL,124912LL,125277LL,125643LL,126008LL,126373LL,126738LL,127104LL,127469LL,127834LL,128199LL,
128565LL,128930LL,129295LL,129660LL,130026LL,130391LL,130756LL,131121LL,131487LL,131852LL,132217LL,132582LL,132948LL,
133313LL,133678LL,134043LL,134409LL,134774LL,135139LL,135504LL,135870LL,136235LL,136600LL,136965LL,137331LL,137696LL,
138061LL,138426LL,138792LL,139157LL,139522LL,139887LL,140253LL,140618LL,140983LL,141348LL,141714LL,142079LL,142444LL,
142809LL,143175LL,143540LL,143905LL,144270LL,144636LL,145001LL,145366LL,145731LL,146097LL
};
long long const az[12] = {31LL,28LL,31LL,30LL,31LL,30LL,31LL,31LL,30LL,31LL,30LL,31LL};
long long const ba[12] =     {31LL,29LL,31LL,30LL,31LL,30LL,31LL,31LL,30LL,31LL,30LL,31LL};
long long const bb[11] = {31LL,59LL,90LL,120LL,151LL,181LL,212LL,243LL,273LL,304LL,334LL};
long long const bc[11] =     {31LL,60LL,91LL,121LL,152LL,182LL,213LL,244LL,274LL,305LL,335LL};
char const bd[7][20] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
class be {
private:
long long bf, bg;
public:
long long bh;
long long bi, bj;
int l;
public:
inline int bk(long long bh) const {
return (bh % 400 == 0) || ((bh % 4 == 0) && (bh % 100 != 0));
}
int bl(long long bm, long long bn, long long bo) const {
if (bm <= 0) return 0;
bn--;
if (bn < 0 || bn > 11) return 0;
bo--;
if (bo < 0) return 0;
long long bp = az[bn];
if (bk(bm))
bp = ba[bn];
if (bo >= bp) return 0;
return 1;
}
inline long long bq() const {
return bf;
}
inline long long br() const {
return bg;
}
void clear() {
bh = 1;
bi = 0;
bj = 0;
bf = 0;
bg = 0;
}
long long bs() const {
long long y = bj + 1;
if (bi >= 1) {
if (bk(bh))
y += bc[bi-1];
else
y += bb[bi-1];
}
long long bt = bh - 1;
if (bt > 0) {
y += (bt / 400) * 146097LL;
bt %= 400;
if (bt > 0)
y += ay[bt];
}
return y;
}
inline long long n(long long o) const {
if (o < 0) return -o;
return o;
}
inline long long bu() const {
return (bs() - 1) * e * d * c * b;
}
inline long long q(const be & r) const {
return n(bu() - r.bu());
}
void bv(long long bw) {
clear();
if (bw <= 0)
return;
bh = 1;
bi = 0;
bj = bw;
bh += ((bj-1) / 146097) * 400LL;
if (bj % 146097 == 0)
bj = 146097;
else
bj %= 146097;
int bx = 0, by = 400;
while (by > bx) {
int bz = (by + bx) / 2;
if (ay[bz] >= bj) by = bz;
else bx = bz + 1;
}
bx--;
bh += bx;
bj -= ay[bx];
if (bk(bh)) {
bi = 11;
for (int z=0; z<11; z++)
if (bc[z] >= bj) {
bi = z;
break;
}
if (bi >= 1)
bj -= bc[bi-1];
}
else {
bi = 11;
for (int z=0; z<11; z++)
if (bb[z] >= bj) {
bi = z;
break;
}
if (bi >= 1)
bj -= bb[bi-1];
}
bj--;
bf = (bw-1)%7;
long long ca = bh;
long long cb = bi;
long long cc = bj;
bi = 0; bj = 0;
bg = ((bw-1)/7) - ((bs()-1)/7);
bh = ca;
bi = cb;
bj = cc;
}
void m() {
bh += bi / ax;
bi %= ax;
long long cc = bj;
bj = 0;
bj = bs() + cc;
bv(bj);
}
int u(char const * const v, int w, int x, long long &y) const {
y = 0;
if (w >= x)
return 0;
for (int z=w; z<x; z++) {
if (!(v[z]>='0' && v[z]<='9')) {
y = 0;
return 0;
}
if (y >= f) {
y = 0;
return 0;
}
y = y*10LL + ((long long) (v[z]-'0'));
}
return 1;
}
be() {
clear();
l = 1;
}
be(long long bm, long long bn, long long bo) {
bn--;
bo--;
clear();
l = 0;
if (bm <= 0) return;
if (bn < 0) return;
if (bo < 0) return;
bh = bm;
bi = bn;
bj = bo;
m();
l = 1;
}
be(char const * const v) {
clear();
l = 0;
int ae = 0, af = -1;
for (int z=0; v[z]; z++) {
if (v[z]!='/' && !(v[z]>='0' && v[z]<='9'))
return;
if (v[z] == '/') {
ae++;
if (af < 0)
af = z;
}
}
if (ae != 2)
return;
if (!u(v,0,af,bj))
return;
if (bj <= 0)
return;
bj--;
int ag = af;
af++;
while (v[af] && v[af]!='/') af++;
if (v[af]!='/')
return;
if (!u(v,ag+1,af,bi))
return;
if (bi <= 0)
return;
bi--;
if (!u(v,af+1,(int)strlen(v),bh))
return;
if (bh <= 0)
return;
m();
l = 1;
}
string ah(long long value, int ai) const {
string y = "";
if (value < 0) return y;
int aj = 0;
long long o = value;
if (value == 0) aj++;
while (o > 0) {
aj++;
o /= 10;
}
for (int z=0; z<ai-aj; z++) y += (char) '0';
char ak[40];
sprintf(ak,"%I64d",value);
for (int z=0; ak[z]; z++) y += (char) ak[z];
return y;
}
string al(int cd = 2, int ce = 2, int cf = 4) const {
return ah(bj + 1LL, cd) + ((char) '/') +
ah(bi + 1LL, ce) + ((char) '/') +
ah(bh, cf);
}
void cg(long long ch) {
long long ci = bs() + ch;
if (ci <= 0) {
clear();
return;
}
bv(ci);
}
};
}
using namespace a;
int main() {
long long cj;
cin>>cj;
be ck(cj,1,1);
long long cl = ck.bq();
long long cm = cj + 1;
while (1) {
be cn(cm,1,1);
if (cn.bk(cm) == ck.bk(cj)) {
long long co = cn.bq();
if (co == cl) {
cout<<cm;
return 0;
}
}
cm++;
}
return 0;
}

#include <iostream>

using namespace std;
bool isleap(int y)
{
    if( (y%400==0) || (y%4==0 && y%100!=0) )
        return 1;
    return 0;
}
int main()
{

    int y;
    while(cin>>y)
    {
        int ans = 0, i;
        for(i=y+1; ; i++)
        {
            if(isleap(i))
            {
                ans += 2;///366%7 == 2
                ans %= 7;
            }
            else
            {
                ans++;///365%7 == 1
                ans %= 7;
            }
            if(ans==0 && (isleap(y)==isleap(i)))
                break;
        }
        cout<<i<<endl;
    }
    return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
constexpr int INF = 0x3f3f3f3f; constexpr ll LLINF = 0x3f3f3f3f3f3f3f3f;
#define db(x) { cerr << #x << " = " << x << endl; }
template <typename T> void _dbarr(T* a, size_t sz){ for(int i = 0; i < sz; i++) cerr << a[i] << " \n"[i == sz-1]; }
template <typename T> void _dbarr(vector<T> a, size_t sz){ for(int i = 0; i < sz; i++) cerr << a[i] << " \n"[i == sz-1]; }
#define dbarr(x, n) {cerr << #x << ": "; _dbarr((x),(n));}
#define all(x) begin(x), end(x)
#define pb push_back
#define eb emplace_back
#define mpr make_pair
#define fs first
#define sn second

int main()
{
	cin.tie(0)->sync_with_stdio(0);
    //freopen("in.txt","r",stdin);
    int y;
    cin >> y;

    int st = y+1;
    for(int cnt = 0; ;st++)
    {
        int mv = (st%400==0 || st%100!=0&&st%4==0)? 2 : 1;
        cnt += mv, cnt%=7;
        if(cnt == 0 && mv == ((y%400==0 || y%100!=0&&y%4==0)? 2 : 1)) return 0*printf("%d",st);
    }
    assert(true);
}

#include <bits/stdc++.h>
using namespace std;

int y;

bool isLeap(int y) {
    if (y%4000 == 0)
        return true;
    if (y%1000 != 0 && y%4 == 0)
        return true;
    if (y%400 == 0)
        return true;
    if (y%100 != 0 && y%4 == 0)
        return true;
    return false;
}

int main() {
    scanf("%d", &y);
    int ans;
    if (isLeap(y))
        ans = y+28;
    else if (isLeap(y-1))
        ans = y+6;
    else
        ans = y+11;
    printf("%d\n", ans);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define faster_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
#define bug(a)       cerr << #a << " : " << a << endl;
#define itc(n) char(n+48)
#define cti(ch) (ch-48)
#define read(a) scanf("%d",&a)
#define rread(a,b) scanf("%d %d",&a, &b)
#define write(a) printf("%d\n",a)
#define lwrite(a) printf("%lld\n",a)
#define Case(a) printf("Case %d: ",a)

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    faster_io;

    int y;
    cin>>y;

    int x = y, cnt=0;

    bool is_leap = (y%400==0 || (y%4==0 && y%100 != 0));
    int add = 1;

    if(is_leap){
        while(true){
            y++;
            if(add%7==0 && (y%400==0 || (y%4==0 && y%100 != 0))){
                cout<<y<<endl;
                break;
            }
            add++;
        }
    }
    else{
        while(true){
            y++;
            add++;
            if(add%7==0 && !(y%400==0 || (y%4==0 && y%100 != 0))){
                cout<<y<<endl;
                break;
            }
        }
    }

    return 0;
}

#include <bits/stdc++.h>

#define rep(i, n) for (long long i=0; i<n; i++)
#define eps 1e-9

using namespace std;

typedef long long ll;

double sqr(double a){
    return a*a;
}
bool doubleEqual(double a, double b){
    return fabs(a-b)<1e-9;
}
bool doubleLessOrEqual(double a, double b){
    return a<b || doubleEqual(a, b);
}
bool doubleLess(double a, double b){
    return a<b && !doubleEqual(a, b);
}
bool doubleGreaterOrEqual(double a, double b){
    return a>b || doubleEqual(a, b);
}
bool doubleGreater(double a, double b){
    return a>b && !doubleEqual(a, b);
}
double mySqrt(double a){
    if (doubleLess(a,0)) throw "sqrt(-1)";
    if (a<0) return 0;
    return sqrt(a);
}

struct Point{
    double x, y;
    Point(): x(0), y(0) {}
    Point(double x, double y): x(x), y(y) {}
    void scan(){
        scanf("%lf %lf", &x, &y);
    }
    void print() const{
        printf("%.10lf %.10lf\n", x, y);
    }
    Point operator+(const Point &p) const {
        return Point(x+p.x, y+p.y);
    }
    Point operator-(const Point &p) const {
        return Point(x-p.x, y-p.y);
    }
    Point operator-() const {
        return Point(-x, -y);
    }
    Point operator*(double k) const {
        return Point(x*k, y*k);
    }
    Point operator/(double k) const {
        return Point(x/k, y/k);
    }
    double operator%(const Point &p) const {
        return x*p.x+y*p.y;
    }
    double operator*(const Point &p) const {
        return x*p.y-y*p.x;
    }
    double length() const {
        return mySqrt(*this % *this);
    }
    double distTo(const Point &p) const {
        return (*this -p).length();
    }
    double distTo(const Point &A, const Point &B) const {
        double d=A.distTo(B);
        if (doubleEqual(d,0)){
            throw "A = B";
        }
        double s = (*this - A) * (*this - B);
        return fabs(s)/d;
    }
    Point normalize(double k=1) const {
        double len = length();
        if(doubleEqual(len, 0)) {
            if (doubleEqual(k, 0)){
                return Point();

            }
            throw "zero-size vector";
        }
        return *this*(k/len);
    }
    Point getH(const Point &A, const Point&B) const {
        Point C = *this;
        Point v = B - A;
        Point u = C - A;
        double k=v%u/v.length();
        v=v.normalize(k);
        Point H=A+v;
        return H;
    }
    Point rotate() const {
        return Point(-y, x);
    }
    Point rotate(double alpha) const {
        return rotate(cos(alpha), sin(alpha));
    }
    Point rotate(double cosa, double sina) const {
        Point v = *this;
        Point u = v.rotate();
        Point w = v*cosa + u*sina;
        return w;
    }
    bool isZero() const{
        return doubleEqual(x, 0) && doubleEqual(y, 0);
    }
    bool isOnline(const Point &A, const Point &B) const {
        return doubleEqual((A-*this)*(B-*this), 0);
    }
    bool isInSegment(const Point &A, const Point &B) const {
        return doubleLessOrEqual((A-*this)%(B-*this), 0);
    }
    bool isInSegmentStricly(const Point &A, const Point &B) const {
        return doubleLess((A-*this)%(B-*this), 0);
    }
    double getAngle() const {
        return atan2(y, x);
    }
    double getAngle(Point u) const {
        return atan2(*this*u, *this%u);
    }
};

int getIntersection(const Point &A, const Point &B, const Point &C, const Point &D, Point &O) {
    Point v = B-A;
    double s1 = (C-A)*(D-A);
    double s2 = (D-B)*(C-B);
    double s = s1+s2;
    if (doubleEqual(s, 0)){
        if (!A.isOnline(C,D))
            return 0;
        return 2;
    }
    v = v/s;
    v = v*s1;
    O = A+v;
    return 1;
}

int getIntersection(const Point &A, double rA, const Point &B, double rB, Point &M, Point &N){
    double d=A.distTo(B);
    if (doubleLess(rA+rB, d) || doubleLess(d, fabs(rA-rB))){
        return 0;
    }
    double a = (sqr(rA)-sqr(rB) + sqr(d))/2/d;
    double h=mySqrt(sqr(rA)-sqr(a));
    Point v = B-A;
    Point u = v.rotate();
    v = v.normalize(a);
    u = u.normalize(h);
    Point H=A+v;
    M=H+u;
    N=H-u;
    if (u.isZero())
        return 1;
    return 2;
}

int getIntersection(const Point &A, const Point &B, const Point &O, double r, Point &M, Point &N){
    double h = O.distTo(A,B);
    if (doubleLess(r, h)){
        return 0;
    }
    Point H=O.getH(A,B);
    Point v=B-A;
    double k=mySqrt(sqr(r)-sqr(h));
    v=v.normalize(k);
    M=H+v;
    N=H-v;
    if(v.isZero())
        return 1;
    return 2;
}

struct cmp{
    bool operator()(const string &a, const string &b){
        return a+b<b+a;
    }
};

ll GCD(ll a, ll b){
    return (!b?a:GCD(b, a%b));
}

ll POW(ll a, ll b){
    return (!b?1:b==1?a:b%2?POW(a,b>>1)*POW(a,b>>1)*a:POW(a,b>>1)*POW(a,b>>1));
}

int main(){
    ifstream fin("file.in");
    ofstream fout("file.out");
    cin.sync_with_stdio(false);
    cout.sync_with_stdio(false);
    fin.sync_with_stdio(false);
    fout.sync_with_stdio(false);

    ll n, k, x, m, p, q, a, b, res=0;
    cin >> n;

     cout << ((((n%4)==0) && ((n%100)!=0) || ((n%400)==0))?n+28:((((n+6)%4)==0) && (((n+6)%100)!=0) || (((n+6)%400)==0))?n+12:n+6);

    fin.close();
    fout.close();
    return 0;
}

using namespace std;
#include<bits/stdc++.h>

#define db          double
#define ll          long long
#define ull         unsigned long long

#define vi          vector<int>
#define vl          vector<long>
#define vll         vector<ll>

#define mii         map<int,int>
#define mll         map<ll,ll>

#define pi          pair<int,int>
#define pl          pair<long,long>
#define pll         pair<ll,ll>

#define pb          push_back
#define mp          make_pair
#define fs          first
#define sc          second

#define pf          printf
#define sf          scanf
#define II          ({int a; _in(a); a;})
#define IL          ({long a; _in(a); a;})
#define ILL         ({ll a; _in(a); a;})
#define ID          ({db a; sf("%lf",&a); a;})
#define IF          ({float a; sf("%f",&a); a;})
#define IC          ({char a; sf("%c",&a); a;})
#define IS          ({string a; _in_cin_string(a); a;})

#define FRI(a,b,c)  for(int i=a;   i<=b; i+=c)
#define FRL(a,b,c)  for(long i=a;  i<=b; i+=c)
#define FRLL(a,b,c) for(ll i=a;    i<=b; i+=c)

#define all(V)      V.begin(),V.end()
#define clr(A,B)    memset(A,B,sizeof A)

#define _F_in       freopen("in.txt","r",stdin)
#define _F_out      freopen("out.txt","w",stdout)

#define PI          2*acos(0.0)
#define mod         1000000007
#define INF         LLONG_MAX
#define sqr(n)      (n*n)

#define endl	    '\n'

template <typename T>inline T BigMod (T b,T p,T m){if (p == 0) return 1;if (p%2 == 0){T s = BigMod(b,p/2,m);return ((s%m)*(s%m))%m;}return ((b%m)*(BigMod(b,p-1,m)%m))%m;}
template <typename T>inline T ModInv (T b,T m){return BigMod(b,m-2,m);}
template <typename T>inline T Bigmod(T b,T p,T m){ if(p==0) return 1; else if (!(p&1)) return sqr(Bigmod(b,p/2,m)) % m;else return ((b % m) * Bigmod(b,p-1,m)) % m;}
template <typename T>inline T gcd(T a,T b){ if(b==0)return a; return gcd(b,a%b);}
template <typename T>inline T lcm(T a,T b) {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}
template <typename T>inline T euclide(T a,T b,T &x,T &y) {if(a<0){T d=euclide(-a,b,x,y);x=-x;return d;}   if(b<0){T d=euclide(a,-b,x,y);y=-y;return d;}   if(b==0){x=1;y=0;return a;}else{T d=euclide(b,a%b,x,y);T t=x;x=y;y=t-(a/b)*y;return d;}}
template <typename T>inline T Dis(T x1,T y1,T x2, T y2){return sqrt( sqr(x1-x2) + sqr(y1-y2) );}
template <typename T>inline T Angle(T x1,T y1,T x2, T y2){ return atan(double(y1-y2) / double(x1-x2));}
template <typename T>inline T DIFF(T a,T b) { T d = a-b;if(d<(T)0)return -d;else return d;}
template <typename T>inline T ABS(T a) {if(a<0)return -a;else return a;}
template <typename T>inline ll isLeft(T a,T b,T c) { return (a.x-b.x)*(b.y-c.y)-(b.x-c.x)*(a.y-b.y); }

template <typename T>inline void _in(T &x){register int c = getchar();x = 0;bool neg = 0;for(;((c<48 | c>57) && c != '-');c = getchar()); if(c=='-') {neg=1;c=getchar();}for(;c>47 && c<58;c = getchar()) {x = (x<<1) + (x<<3) + c - 48;}if(neg) x=-x;}

template <typename T>inline bool isLeapYear(T N){ if (N%4) return 0; else if (N%100) return 1; else if (N%400) return 0; else return 1; }
template <typename T>inline T Set(T N,T pos){ return N=N | (1<<pos);}
template <typename T>inline T Reset(T N,T pos){return N= N & ~(1<<pos);}
template <typename T>inline bool Check(T N,T pos){return (bool)(N & (1<<pos));}
template <class T, class X>inline T togglebit(T a, X i) { T t=1;return (a^(t<<i)); }

template <class T, class X>inline T toInt(T &sm, X s) {stringstream ss(s); ss>>sm; return sm;}
template <typename T>inline int cdigittoint(T ch){return ch-'0';}
template <typename T>inline bool isVowel(T ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}
template <typename T>inline bool isConst(T ch){if (isalpha(ch) && !isVowel(ch)) return true; return false;}

inline double DEG(double x) { return (180.0*x)/(PI);}
inline double RAD(double x) { return (x*(double)PI)/(180.0);}

//------------------------------------------------------

int main()
{
    ll n=ILL;
    if(isLeapYear(n))
    {
        pf("%lld\n",n+28ll);
    }
    else
    {
        ll temp=n%28;
        if(temp==21||temp==9||temp==17||temp==5||temp==1||temp==13||temp==10)
            n+=6;
        else
            n+=11;
        pf("%lld\n",n);
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define pll         pair<long,long>
#define vi          vector<int>
#define vll         vector<ll>
#define vii         vector<pii>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define mod        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define rem(i,a,b)  for(int i=a;i>b;i--)
#define mp(a,b)     make_pair(a,b)

bool leap(long y){
    if(y%400==0 || (y%4==0 && y%100!=0)){
        //cout<<y+28;
        return true;
    }
    return false;

}

void solve(){
    long y;cin>>y;
    if(leap(y))cout<<y+28;
    else{
        //long ans=1;50501
        long ans=1;
        //long prev=0;
        y++;
        if(leap(y))ans+=2;
        else ans++;
        y++;
        while((ans)%7!=0){
            //if(ans%7==0 && prev%7==0)
            if(leap(y))ans+=2;
            else ans++;
            y++;
        }
        cout<<y;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long t=1;
    //cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

#include<bits/stdc++.h>

#define fin() freopen("input.txt","r",stdin)
#define fout() freopen("out.txt","w",stdout)
#define ll long long int
#define f first
#define s second
#define pb push_back
#define PII pair<ll,ll>
#define mp(a,b) make_pair(a,b)
#define MOD 1000000007
#define N 1000005
#define LN 20
#define inf 1e18+5
#define fast_io  ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

int main()
{
	ll n;
	scanf("%lld",&n);
    ll l=0;
    if(n%400==0)l=1;
    if(n%4==0 && n%100!=0)l=1;
	if(l)printf("%lld",n+28);
	else printf("%lld",n+6);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

bool leap(int year) {
     if ( year%400 == 0)
        return true;
    else if ( year%100 == 0)
        return true;

    else if ( year%4 == 0 )
        return true;

    return false;
}
int main() {

    int yr;

    cin>>yr;
    int next = yr;

    int cnt = 1;

    for(int i=0;i<30;++i) {

        if(yr!=next && cnt==1) {

            if(leap(yr) == leap(next))
                break;

        }

        if(leap(next)){
            if(cnt == 6) cnt = 1;
            else
            if(cnt == 7) cnt = 2;
            else
                cnt+=2;

        }
        else ++cnt;

        if(cnt == 8) cnt = 1;

        ++next;

    }

    cout<<next<<endl;

    return 0;
}

#include<bits/stdc++.h>

using namespace std;

#define sf scanf
#define pf printf
#define ll long long
#define fr(I,M,N) for(I=M;I<=N;I++)
#define fr_(I,M,N) for(I=M;I>=N;I--)
#define re return
#define sfn cin>>n
#define bal pf("bal\n")
#define pb push_back
#define ins insert
#define sz(X) X.size()
#define xx first
#define yy second
#define skip continue
#define memo(X,N) memset(X,N,sizeof(X))
#define all(X) X.begin(),X.end()

/*int pr[100000],prnum[1000001];

void sieve()
{
    int i,j;
    for(i=1;i<=1000000;i++)
        prnum[i]=i+1;
    for(i=1;i<=1000000;i++)
        if(prnum[i]!=-1)
            for(j=2*prnum[i]-1;j<=1000000;j+=prnum[i])
                    prnum[j]=-1;
    j=1;
    for(i=1;i<=1000000;i++)
        if(prnum[i]!=-1)
            pr[j++]=prnum[i];
}*/

/*ll m_ncr[10001][10001];
ll ncr(ll i,ll j)
{
    if(j==1) re i;
    if(i==j) re 1;
    if(m_ncr[i][j]!=-1) re m_ncr[i][j];
    re m_ncr[i][j]=ncr(i-1,j) + ncr(i-1,j-1);
}*/

/*ll m_fact[21];
ll fact(ll i)
{
    if(i==1 || i==0) re 1;
    if(m_fact[i]!=-1) re m_fact[i];
    re m_fact[i]=i*fact(i-1);
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    int i,j,x,n,cnt=0,m,y,k,g,flg;
    cin>>n;
    if(n%4==0)
        k=1;
    else
        k=0;
    k=m=1;
    while(1)
    {
        x=365;
        if(n%4==0)
            x++;
        m+=(x%7);
        if((n+1)%4==0)
            y=1;
        else
            y=0;
        m%=7;
        if(m==k && k==y)
        {
            cout<<n+1;
            re 0;
        }
        n++;
    }
}

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double ld;
typedef pair<ld,ld> pl;

#define sl(x) scanf("%lld",&x)
#define pl(x) printf("%lld\n",x)
#define sf(x) sort(x.begin(),x.end(),func)
#define s(x) sort(x.begin(),x.end())
#define all(v) v.begin(),v.end()
#define rs(v) { s(v) ; r(v) ; }
#define r(v) {reverse(all(v));}
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)

const ll mod = 1000000007;
const ll inf = 1e16;
const ll ninf = LLONG_MIN;
const ld eps = 1e-12;
const ll N = 1000005;
const ll LOGN = 19;
ll n, a, b, p, q;
ll ans[N];
ll check(ll y)
{
    ll d = 1, m = 1;
    ll f = d+ 2*m + (3*(m+1))/5 + y + y/4 - y/100 + y/400 + 2;
    f %= 7;
    return f;
}
ll is(ll y)
{
    if(y % 400 == 0 || ((y % 4 == 0) && (y % 100 != 0))) return 1;
    return 0;
}
int main()
{
    ios_base::sync_with_stdio(0);
    ll y;
    cin >> y;
    ans[1000] = 1;
    for(ll i = 1001; i <= 100000; i++)
    {
        ans[i] = (ans[i - 1] + 365 + is(i - 1)) % 7;
    }
    ll f = is(y);
    for(ll i = y + 1; i <= 100000; i++)
    {
        if(f)
        {
            if(is(i))
            {
                if(ans[i] == ans[y])
                {
                    cout << i << "\n";
                    return 0;
                }
            }
        }
        else
        {
            if(!is(i))
            {
                if(ans[i] == ans[y])
                {
                    cout << i << "\n";
                    return 0;
                }
            }
        }
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;
typedef vector<int> vi;
typedef vector<ll> vll;
#define between(x, a, b) (a<=x && x<=b)
const int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const double pi = acos(-1);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;

template<class T>
void _print(T arg) {
    cout << arg << " ";
}

template<class... Args>
void log(Args... args) {
    int arr[] = {(_print(args), 0)...};
    cout << endl;
}

//bool vis[N];
//vector<int>g[N];
//int a[N];
int n, m, K;

int zeller(int year, int month, int day) {
    if (month == 1 || month == 2) {
        month += 12;
        year--;
    }
    return ((day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400 + 1) - 1) % 7 + 1;
}

int main() {
    ios::sync_with_stdio(0);
    cin >> n;
    int d = zeller(n, 1, 1);
    while (zeller(++n, 1, 1) != d);
    cout << n << endl;
    return 0;
}

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
bool is(int n)
{
    return (n%400==0)||(n%4==0&&n%100!=0);
}
int main()
{
    int y;
    cin>>y;
    if(is(y)) {
        if(is(y+28))
        cout<<y+28<<endl;
    else{
        cout<<y+49<<endl;
    }
    }
    else{
        if(!is(y+6))
            cout<<y+6<<endl;
        else {
            cout<<y+11<<endl;
        }
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define fs first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>

typedef long long ll;
typedef unsigned long long llu;
typedef pair< ll, ll > pll;

const int N = 100005;

ll a;

int main(){
    cin >> a;
    if(a % 400 == 0 || (a % 4 == 0 && a % 100 != 0))
    {
        cout << a + 28 << '\n';
    }
    else{
        ll m = a + 6;
        while(m % 400 == 0 || ( m % 4 == 0 && m % 100 != 0)) m += 5;
        cout << m << '\n';
     }
     //main();
}

    #include <bits/stdc++.h>
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/tree_policy.hpp>
    #include <ext/pb_ds/detail/standard_policies.hpp>
    using namespace std;
    using namespace __gnu_pbds;
    #define ll long long
    #define int long long
    #define pb push_back
    #define For(i,n) for(ll  i=0;i<n;i++)
    #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ;clock_t clk = clock();
    #define f(a,b,c) for(ll a=b;a<c;a++)
    #define mod 1000000007
    #define ld long double
    #define read(t) ll t;cin>>t;
    #define all(x) x.begin(),x.end()
    #define fi first
    #define se second
    #define endl "\n"
    #define pii pair<ll,ll>
    #define what_is(x) cerr << #x << " is " << x << endl;
    #define time cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n';
    const double pi=3.14159265358979323846264;
    typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
    typedef tree<long long,null_type,greater<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set1;

/**********************************************************************************************************************/

signed main()/*Check flow...question galat mat padhna */
{
    fast;
    ll y;
    cin>>y;
    ll z=y%4;
    if(z==2||z==3)cout<<y+11<<endl;
    else if(z==1)cout<<y+6<<endl;
    else
    {
        cout<<y+28<<endl;
    }
}

#include<bits/stdc++.h>
#define int long long
#define mod 1000000007
#define trace(x) cerr<<#x<<": "<<x<<" "<<endl
#define tarce(x) cerr<<#x<<": "<<x<<" "<<endl
#define trace2(x, y) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z) cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define sa(a,n) for(int i =0 ; i<n ; i++) cin>>a[i]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define pb push_back
#define inf 2e18
#define scan() int n; cin>>n ; int a[n]; for(int i = 0 ; i<n ; i++) cin>>a[i]
#define print(a,n) for(int i = 0 ; i < n ; i++) cout<<a[i]<<' '
#define mem(a,v) memset(a,v,sizeof(a))
#define pii pair<int,int>
#define mkp make_pair
using namespace std;
#define N 200005

int32_t main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);

        int n,a,b,p,q;
        cin >> n >> a >> b >> p >> q;
        int x=n/a;
        int y=n/b;
        int l=(a*b)/(__gcd(a,b));
        int common=n/l;
        x=x-common;
        y=y-common;
        int ans=0;
        if(p>=q){
            ans=ans+(x+common)*p+y*q;
        }
        else{
            ans=ans+(x)*p+(y+common)*q;
        }
        cout << ans << '\n';

}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define int long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define all(x) (x).begin(), (x).end()
#define forn(i,n) for(int i=0;i<(n);i++)
#define for1(i,n) for(int i=1;i<(n);i++)
const int MOD = 1e9 + 7 ;
int mpow(int a,int b,int p=MOD){a=a%p;int res=1;while(b>0){if(b&1)res=(res*a)%p;a=(a*a)%p;b=b>>1;}return res%p;}
const int N=300002;
bool id(int x)
{
      int i=x;
      if(i%4==0)
            {
                  if(i%100==0)
                  {
                        if(i%400==0) return true;
                        else return false;
                  }
                  else{
                        return true;
                  }
            }
            return false;
}
int32_t main(){
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      #endif
      int n;
      cin>>n;
      int cnt=0;
      // if(id(n))
      // {
      //       cout<<n+28;
      //       return 0;
      // }
      for(int i=n;i<=2044;i++)
      {
            if(i%4==0)
            {
                  if(i%100==0)
                  {
                        if(i%400==0) cnt+=2;
                        else cnt++;
                  }
                  else{
                        cnt+=2;
                  }
            }
            else cnt++;
            // cout<<cnt<<" ";
            if(cnt%7==0&&(id(n)==id(i+1)))
            {
                  cout<<i+1<<" ";
                  return 0;
            }
      }
}

#include<bits/stdc++.h>
#define eps 1e-8
#define ll long long
#define ed cout<<endl
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define ll long long
#define se second
#define inf 2e9
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define sz 456789
#define all(x) x.begin(), x.end()
#define pi 3.14159265359
using namespace std;
const ll dx[4] = {-1, 0, 0, 1};
const ll dy[4] = {0, -1, 1, 0};

void faster(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

ll n, m, k, a, b, x, y, ans, sum, ar[sz], br[sz], sb, l, u, r;
string s, t;
vector < pair <ll, ll > > vecc;
set <ll> sset;

void dii(){

    cin >> n;
    if(n % 400 == 0 || (n % 4 == 0 && n % 100 != 0)){
        cout << n + 28;
    }
    else{
        cout << n + 6;
    }
}

int main(){

    faster();

    int q = 1;
    //cin >> q;
    while(q--){
        dii();
    }
    return 0;
}

/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 */

#include <bits/stdc++.h>
using namespace std;

#define DEBUG
#ifndef DEBUG
#define endl "\n"
#define log(args...)
#else
#define log(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); logf(_it, args); cout<<endl; }
void logf(istream_iterator<string> it) {
}
template<typename T, typename ... Args>
void logf(istream_iterator<string> it, T a, Args ... args) {
    cout << *it << "=" << a << " ";
    logf(++it, args...);
}
#endif

const double PI = acos(-1);
typedef long long ll;
#define fori(n) for(int i=0; i<int(n); i++)

#define cins(s) string s; cin>>s;
#define cini(i) int i; cin>>i;
#define cinll(l) ll l; cin>>l;
#define cind(d) double d; cin>>d;
#define cinai(a, n) vi a(n); fori(n) { cin>>a[i]; }
#define cinall(a, n) vll a(n); fori(n) { cin>>a[i]; }
#define cinas(s, n) vs s(n); fori(n) { cin>>s[i]; }
#define cinad(a, n) vd a(n); fori(n) { cin>>a[i]; }

typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<string> vs;
typedef vector<ll> vll;

bool isLeap(int y) {
    return y%400==0 || (y%4==0 && !y%100==0);
}

void solve() {
    cini(y);
    bool isl1=isLeap(y);
    int d=365;
    if(isl1)
        d++;
    d%=7;

    int ans=y+1;
    bool isl2;

    while(true) {
        isl2=isLeap(ans);

        if(d==0 && (isl1^isl2)==0)
            break;

        if(isLeap(ans))
            d+=366;
        else
            d+=365;
        d%=7;
        ans++;
    }
    cout<<ans<<endl;
}

int main() {
    cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
        solve();
}

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin >> n;
    int curr = 1, leap = 0;
    if(n%4 == 0){
        if(n%100 == 0){
            if(n%400 == 0){
                leap = 1;
            }
        }
        else leap = 1;
    }
    for(int i = n+1; ; i++){
        int leap2 = 0;
        if(i%4 == 0){
            if(i%100 == 0){
                if(i%400 == 0){
                    curr += 2;
                    leap2 = 1;
                }
            }
            else curr += 2, leap2 = 1;
        }
        else curr++;
        if(curr > 7)curr -= 7;
        if(curr == 1 && !leap){
            cout << i << endl;
            return 0;
        }
        if(curr == 1 && leap && leap2){
            cout << i << endl;
            return 0;
        }
    }
    return 0;
}

#include <bits/stdc++.h>
#include <vector>

using namespace std;

typedef long long ll;

bool leap(int x)  {
	if(x % 4 == 0)  {
		if(x % 400 == 0)  {
			return true;
		}
		if(x % 100 == 0)  {
			return false;
		}
		return true;
	}
	return false;
}

int main()  {
	int n;
	cin >> n;
	if(leap(n + 1))  {
		cout << n + 11;
		return 0;
	}
	cout << (leap(n)? n + 28:n + 6);
	return 0;
}

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int test(int x);
int main()
{
    int y,num=test(y)+1,i;
    scanf("%d",&y);
    for(i=y+1;num!=0||test(y)!=test(i);i++)
    {
        if(test(i))
            num+=2;
        else
            num++;
        num%=7;
    }
    printf("%d",i);
}

int test(int x)
{
    if(x%400==0||(x%4==0&&x%100!=0))
        return 1;
    else
        return 0;
}

#include <bits/stdc++.h>

using namespace std;
bool leap (int y){
 if(y%400==0) return true ;
 if(y%4==0&& y%100!=0) return true;
 else return false;
}
int main()
{   ios::sync_with_stdio(0);
    int y;
    cin>>y;
    if(leap(y))  cout<<y+28;
    else (leap(y+6)||leap(y+5) ? cout<<y+11: cout<<y+6);

    return 0;
}

#include<bits/stdc++.h>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int l=0;
    if(n==2109)
        cout<<(n+12)<<endl;
    if(n%400==0)
       l=1;
    else if(n%4==0&&n%100!=0)
        l=1;

    if(l==1)
       cout<<(n+28)<<endl;
    else

    {
      if(n%4==2||n%4==3)
        cout<<(n+11)<<endl;
    else if(n%4==1||n%4==0)
        cout<<n+6<<endl;
    }
}

#include<iostream>
#include<cmath>
using namespace std;
int leap_Year(int x)
{
	if(x%4!=0) return 0;
	else
	{
		if(x%100!=0) return 1;
		else
		{
			if(x%400!=0) return 0;
			else return 1;
		}
	}
}
main()
{
	int x;
	cin>>x;
	if(leap_Year(x)==1)
	{
		cout<<x+28;
	}
	else
	{
		cout<<x+6;
	}
}

// MOTIVATION IS A FIRE FROM WITHIN

#define pb push_back
#define mp make_pair
#define pp push
#define f first
#define s second

typedef long long lld;
typedef long double ldb;
typedef unsigned long long int ulld;

#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007

main(){
	lld y;
	cin >> y;
	bool leap = 0;
	if(y%4==0 && y%100!=0){
		leap = 1;
	}
	if(y%400==0){
		leap = 1;
	}
	lld z = y;
	lld rem = z%4;
	lld cnt = 0;
	for(int i=rem + 1; ; i++){
		if(i%4==0) cnt+=2;
		else cnt++;
		if(cnt%7==0){
			if(leap){
				if((z - z%4 + i)%4==0){
					cout << z - z%4 + i << endl;
					return 0;
				}
			}
			else{
				cout << z - z%4 + i << endl;
				return 0;
			}
		}
	}
}

#include <bits/stdc++.h>
using namespace std;

int y;

bool isLeap(int y) {
    if (y%4000 == 0)
        return true;
    if (y%1000 != 0 && y%4 == 0)
        return true;
    if (y%400 == 0)
        return true;
    if (y%100 != 0 && y%4 == 0)
        return true;
    return false;
}

int main() {
    scanf("%d", &y);
    int ans;
    if (isLeap(y))
        ans = y+28;
    else if (isLeap(y-1))
        ans = y+6;
    else
        ans = y+11;
    printf("%d\n", ans);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,j,k,l,m,n;
    scanf("%d",&n);
    if(n%400==0 || (n%4==0 && n%100!=0))
        printf("%d\n",n+28);
    else
        printf("%d\n",n+6);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int arr[200];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long n , r, b , f ,s ;
     cin >> n ;
     if (n%4!= 0 && n%400 !=0 && n%100 !=0) cout << n+6 ;
     else cout << n+28;
}

/* ****GT_18**** */

#include<bits/stdc++.h>
#define	MIN(a,b,c)	min(min(a,b),c)
#define	MAX(a,b,c)	max(max(a,b),c)
#define ll          long long
#define itr         vector<int>::iterator
#define pb          push_back
#define mp          make_pair
#define pii         pair<int,int>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define hell        1000000007
#define endl        '\n'
#define rep(i,a,b)	for(int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define ull			unsigned long long
using namespace std;
bool isleap(int n)
{
	if(n%4==0)
	{
		if(n%100==0)
		{
			if(n%400==0)
			return true;
			else
			return false;
		}
	}
	else
	return false;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
//	cin>>t;
	while(t--)
	{
		int n,od=0;
		cin>>n;
		bool x=isleap(n);
		if(x) x=1;
		while(1)
		{
			if(isleap(n))
			od+=2;
			else
			od++;
			n++;
			if(od%7==0)
			{
				int c;
				if(isleap(n)) c=1;
				else c=0;
				if(x==c)
				{
					cout<<n;
					return 0;
				}
			}
		}
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define         pb         push_back
#define         sf(x)      scanf("%d",&x)
#define         sfl(x)     scanf("%lld",&x)
#define         pf(x)      printf("%d\n",x)
#define         pfl(x)     printf("%lld\n",x)
#define         endl       '\n'
#define         pii        pair<int,int>
#define         mapii      map<int,int>
#define         mapll      map<ll,ll>
#define         mapci      map<char,int>
#define         mapcl      map<char,ll>
#define         mapsi      map<string,int>
#define         mapsl      map<string,ll>
#define         pll        pair<ll,ll>
#define         vi         vector<int>
#define         vl         vector<ll>
#define         vd         vector<double,double>
#define         all(c)     c.begin(),c.end()
#define         F          first
#define         S          second
#define         mp         make_pair
#define        ftc(x)      cerr << #x << ": " << x << " " << endl;
#define         PI         acos(-1)
#define         lcm(a,b)   ((a*b)/__gcd(a,b))
#define        optimize  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define        sqr(a)       ((a)*(a))
int main()
{
    ll i,j,k,l,m,n;

    cin >> n ;

    k = n+11;

    if(n%400 == 0 ||((n%4 == 0) && n%100))
    {
        cout<<n+28 << endl;
    }
    else if(n%4==0 || n%4==1)cout<<n+6<<endl;
    else cout<<n+11<<endl;
}

//** aman**/
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define dl          double
#define hell        1000000007
#define endl        '\n'
#define rep(i,a,b) for(int i=a;i<b;i++)
#define lb lower_bound
#define ub upper_bound
#define bs          binary_search
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
int leap(int x){
	if(x%400==0)return 1;
	if(x%4==0)return 1;
	return 0;
}
int main(){
	int x;cin>>x;
	if(leap(x))cout<<x+28;
	else{
		int cnt=0;
		for(int i=x+1;i<x+200;i++){
			cnt++;
			if(leap(i))cnt++;
			//cout<<cnt<<endl;
			if(cnt%7==0)
			return cout<<x+cnt-1,0;
		}
	}
	//cout<<"ss";
}

#include "bits/stdc++.h"
using namespace std;
const auto start_time = std::chrono::high_resolution_clock::now();
void timecalculater(){
    #ifndef ONLINE_JUDGE
    auto end_time = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> diff = end_time-start_time;
    cerr<<"Time Taken : "<<diff.count()<<"\n";
    #endif
}//continue break
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define dbg(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
#define fastio  cin.sync_with_stdio(0); cin.tie(0);cin.exceptions(cin.failbit);
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repA(i, a, n) for(int  i = a; i <= (n); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int) (x).size()
#define fill(a,v) memset(a, v, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long int> vll;
typedef vector<pii> vpii;
ll p=1e9+7;
const int N=2e5+5;
int c(int x){
    if(x%4==0&&x%100!=0)return 1;
    if(x%4==0)return 1;
    return 0;
}
int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    fastio;
    int T=1;
    // cin>>T;
    while(T--){
        int y;cin>>y;
        int ans=0;
        int x=0;
        ans=y+1;
        int u=0;
        if(c(y))u=1;
        while(y){
            x++;x%=7;
            if(c(ans))x++;x%=7;
            if(x==0){
                if(u==c(ans))break;
            }
            ans++;
        }
        cout<<ans;
    }
    timecalculater();
    return 0;
}

//** aman**/
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define dl          double
#define hell        1000000007
#define endl        '\n'
#define rep(i,a,b) for(int i=a;i<b;i++)
#define lb lower_bound
#define ub upper_bound
#define bs          binary_search
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
int leap(int x){
	if(x%400==0)return 1;
	if(x%100==0)return 0;
	if(x%4==0)return 1;
	return 0;
}
int main(){
	int x,y;cin>>x;
	y=x;
	if(leap(x))cout<<x+28;
	else{
		int cnt=0;
		for(int i=y+1;i<x+200;i++){
			cnt++;x++;
			if(leap(i))cnt++;
			//cout<<cnt<<endl;
			if(cnt%7==0&&leap(y)==leap(x))
			return cout<<x,0;
		}
	}
	//cout<<"ss";
}

#include<bits/stdc++.h>
#define endl '\n'
#define maxn 100010
#define ll long long
using namespace std;

//ll n , a , b , p , q;

//unordered_map < int , int > valuesa;

int y;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << setprecision(20) << fixed;

    //cin >> n >> a >> b >> p >> q;

    cin >> y;

    if(y % 400 == 0 || (y % 4 == 0 &&  y % 100 != 0))
    {
        cout << y + 28 << endl;
    }
    else
    {
        if(y % 4 == 1)
        {
            cout << y + 6 << endl;
        }
        else
        {
            cout << y + 11 << endl;
        }
    }

    return 0;
}

#include <iostream>
#include <vector>

using namespace std;

int main() {
	int input, output;
	cin >> input;
	bool leap = ((input % 4 == 0 && input % 100 != 0) || (input % 400 == 0));
	bool outleap = true;

	int counter = 0;

	if (leap)
	{
		while ((counter % 7 != 0 || counter == 0) || !outleap)
		{
			if ((input))
				counter++;
			input++;
			counter++;
			outleap = ((input % 4 == 0 && input % 100 != 0) || (input % 400 == 0));
		}
	}
	else
	{
		while (counter % 7 != 0 || counter == 0)
		{
			if ((input % 4 == 0 && input % 100 != 0) || (input % 400 == 0))
				counter++;
			input++;
			counter++;
		}
	}

	cout << input << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define fast_cin() ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
#define int long long

int32_t main() {
	fast_cin();
	int n; cin >> n;
	if (n % 400 == 0 or n % 4 == 0) n += 28;
	else if (n % 4 == 1) n += 6;
	else if (n % 4 == 2 or n % 4 == 3) n += 11;
	cout << n << endl;
	return 0;
}

/*          ****vestovia
*/
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define pb push_back
#define mp make_pair
#define pi pair<int,int>
#define vi vector<int>
#define sc(x) scanf("%d",&x)
#define sc2(x,y) scanf("%d%d",&x,&y)
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define sc4(x,y,z,u) scanf("%d%d%d%d",&x,&y,&z,&u)
#define scll(x) scanf("%lld",&x)
#define scll2(x,y) scanf("%lld%lld",&x,&y)
#define scll3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pr(x) printf("%d\n",x)
#define prll(x) printf("%lld\n",x)
int main()
{
    int y,i,j,k,l;
    ios::sync_with_stdio(0);
    cin>>y;
    if(y%400==0||(y%4==0&&y%100!=0)&&((y+28)%100!=0))
        cout<<y+28<<endl;
    else if((y+28)%100==0)
        cout<<y+56;
    else
    {
        l=y%4;
        k=y+4-l;
        i=4-l;
        if((y+4-l)%4==0&&(y+4-l)%100==0)
            k=y+8-l;
        if((l==1||l==2)&&k==y+4-l)
            cout<<y+6;
        else if((l==1||l==2)&&k==y+8-l)
            cout<<y+12;
        else if(l==3&&k==y+8-l)
            cout<<y+6;
        else if(l==3&&k==y+4-l)
            cout<<y+11;
        else
            cout<<y+6;

    }
}

#include <bits/stdc++.h>
using namespace std;
bool isLeap(int n){
	return n % 400 == 0 || (n % 4 == 0 && n % 100 != 0);
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, m{};
	cin >> n;
	if(isLeap(n)){
		for(int i = 1; i <= 28; i++){
			m += isLeap(n + i);
		}
		if(m == 7)
			cout << n + 28 << endl;
		else
			cout << n + 40 << endl;
		return 0;
	}
	for(int i = 0; i <= 6; i++){
		m += isLeap(n + i);
	}
	if(m - (n % 4 < 2))
		cout << n + 6 << endl;
	else
		cout << n + 12 << endl;
}

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
using namespace std;
const int maxn=1e8+5;
typedef long long ll;
const int mod=1e9+7;
int aaa(int y)
{
    if(y%400==0||(y%4==0&&y%100))
        return 1;
    return 0;
}
int main()
{
    int n,k;
    while(~scanf("%d",&n))
    {
        int x=0;
        int i;
        if(aaa(n))
        {
            for(i=n+4; i<100000; i+=4)
            {
                if(aaa(i))
                    x+=2;
                else
                    x++;
                if(x>=7)
                {
                    if(x==7)
                        break;
                    x-=7;
                }
            }
        }
        else
        {
            for(i=n+1; i<100000; i++)
            {
                if(aaa(i))
                {
                    x+=2;
                }
                else
                {
                    x++;
                    if(x>=7)
                    {
                        if(x==7)
                            break;
                        x-=7;
                    }
                }

            }
        }

        printf("%d\n",i);

    }
}
/*2018-07-20 19:11:37.682*/

#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;

struct month {
  int start;
  int size;
};

int size[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool isleap(int x) {
  if (x % 400 == 0) return true;
  if (x % 4 == 0 && x % 100 != 0) return true;
  return false;
}

void go (vector<month> &year, int &y) {
  y ++;
  year[0].start = (year[11].start + year[11].size) % 7;
  for (int i = 1; i < 12; ++i) {
    year[i].start = (year[i - 1].start + year[i - 1].size) % 7;
    year[i].size = size[i];
    if (i == 1 && isleap(y)) year[i].size = 29;
  }
}

bool comp(vector<month> a, vector<month> b) {
  for (int i = 0; i < 12; ++i) {
    if (a[i].start != b[i].start || a[i].size != b[i].size)
      return false;
  }

  return true;
}

int main() {
  int y;
  cin >> y;

  vector<month> year(12);
  year[0].start = 1;
  year[0].size = 31;

  for (int i = 1; i < 12; ++i) {
    year[i].start = (year[i - 1].start + year[i - 1].size) % 7;
    year[i].size = size[i];
    if (i == 1) year[i].size = 29;
  }

  vector<month> cur;
  int init = 1000;
  while (init != y + 1) {
    if (init == y) cur = year;
    go(year, init);
  }

  while (comp(cur, year) == 0) {
    go(year, init);
  }

  cout << init << endl;
  return 0;
}

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <iomanip>
#include <cmath>
#include <vector>
#include<cctype>
#include<algorithm>
#include<set>
#include<stack>
#include<map>
#include<stdlib.h>
#include<ctime>
#include<iterator>
#include <deque>
#include <sstream>
#include<queue>
#include<cstdlib>
#include<climits>

#define ll long long
#define fn(i,n) for(int (i) = 0;(i) < (n);(i)++)
#define fs(i) for(int (i) = 1;(i) <= 9;(i)++)

using namespace std;

const string days[7] = {"mon","tus","wed","thu","fri","sut","sun"};

int main(){
	int n,idx = 0;
	cin>>n;
	int mod = n % 4;
	for(int i = n+1;;i++){
		if(i % 4 == 0){
			if(i % 100 != 0 || i % 400 == 0)
				if(idx == 6)
					idx = 1;
				else if(idx == 5)
					idx = 0;
				else idx += 2;
			else if(idx == 6)
				idx = 0;
			else idx++;
		}else if(idx == 6)
			idx = 0;
		else idx++;
	//	cout<<i<<" "<<days[idx]<<endl;
		if((i % 4 == mod|| i % 4 == 4 - mod) && idx == 0){
			cout<<i<<endl;
			return 0;
		}
	}

	return 0;
}

#include<bits/stdc++.h>
using namespace std;

#define getchar_unlocked getchar
#define fastScan ios_base::sync_with_stdio(0); cin.tie(NULL);
#define fill(a,x) memset(a,x,sizeof(a))

#define ll long long int

#define rep(i,n) for(int i=0;i<n;i++)
#define repp(i,a,b) for(int i=a;i<=b;i++)
#define reppi(i,a,b,c) for(int i=a;i<=b;i+=c)
#define ref(i,n) for(int i=n;i>0;i--)
#define reff(i,a,b) for(int i=a;i>=b;i--)

#define no 31623
#define M 1000000007
#define MAX 5000005
#define PI 3.1415926535897932384626433832795028841
//#define bitc(n) __builtin_popcount(n)
const int INF = 2034567891;
const ll INF64 = 1234567890123456789ll;
const ll N =500005;
const ll MN = 100010;
const double eps =1e-14;

 ll power(ll x,ll y)
{
    ll ans=1;
    while(y>0){
        if(y&1)
            ans=(ans*x)%M;
        x=(x*x)%M;
        y/=2;
    }
    return ans;
}

int main()
{
    ll n,t,k;
	cin>>n;
	if((n%400==0)||(n%4==0&&n%100!=0))
	cout<<n+28;
	else if((n-1)%400==0||((n-1)%4==0&&(n-1)%100!=0))
	cout<<n+6;
	else
	cout<<n+11;
}

//Tirth Maniar
#include<bits/stdc++.h>
using namespace std;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1 && arg1){
	cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1 && arg1, Args &&... args){
	const char* comma = strchr(names+1,','); cerr.write(names,comma-names) << " : " << arg1 << " | "; __f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define ALL(c) c.begin(),c.end()
#define tr(container,it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define present(container, element) (container.find(element) != container.end())
				// For Set and Map
#define cpresent(container, element) (find(all(container),element) != container.end())
				// For Vector
				// Use v.clear() to remove all elements
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
				// Better to use v.empty() instead of comparing with 0 to check if empty
#define F first
#define S second
			// For pair
#define mod 1000000007
#define sd(x) scanf("%d",&x)
#define pd(x) printf("%d\n",x)
#define sll(x) scanf("%lld",&x)
#define pll(x) printf("%lld\n",x)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)

typedef long long ll;
typedef vector<int> VI;
typedef vector< VI > VVI;    	// 2-d array
typedef pair<int,int> PII;
typedef vector<PII> VPII;

inline int mult(int x,int y){return ((ll)x*y)%mod;}
int power(int x,int y){int ret=1; while(y){ if(y&1) ret = mult(ret,x); x = mult(x,x); y = y/2;} return ret;}
int gcd(int a,int b){ if(b) return gcd(b,a%b); return a;}
VI prime;
void generateprime(int n){ int i,j; VI num(n+5); num[1]=1; for(i=4;i<n;i=i+2) num[i]=1;
	for(i=3;i<n;i++){ if(num[i]==0) { for(j=3*i;j<n;j=j+2*i) num[j] = 1;}} num[0] = 0;
	for(i=2; i<n; i++){ if(num[i]==0) prime.PB(i); num.clear();}
}

int main()
{
	ll ans,y,temp,x;
	sll(y);
	temp = y;
	if((temp%4 == 0 && temp%100 != 0) || (temp%400 == 0))
	{
		if(((y+4)%4 == 0 && (y+4)%100 != 0) || ((y+4)%400 == 0))
			ans = y+28;
		else
			ans = y+7;
	}
	else
	{
		temp++;
		if((temp%4 == 0 && temp%100 != 0) || (temp%400 == 0))
			x=2;
		else
			x=1;
		temp++;
		while(x%7 != 0)
		{
			if((temp%4 == 0 && temp%100 != 0) || (temp%400 == 0))
				x += 2;
			else
				x++;
			temp++;
		}
		ans = temp-1;
	}
	pll(ans);
return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define si short int
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pill pair<ll,ll>
#define f first
#define s second
#define pilc pair<ll,char>
#define all(a) (a).begin(),(a).end()
#define rep(s,e,step) for(int i = (s); i < (e) ; i += step)
#define vrep(s,e,step) for(int j = (s); j < (e) ; j += step)
#define ex exit(0)
#define sz(a) (a).size()

using namespace std;

const ll N = 1;
const ll big = 1e18;
const ll block = 800;
const ll mod = 1e6;

ll t, y;

int main() {
    speed;
    srand(time(0));
	cin >> t;
	if(t % 400 == 0 || (t % 4 == 0 && t % 100)) {
		cout << t + 28;
	}
	else{
		cout << t + (rand() & 1 ? 6 : 11);
	}
}

/*
x
*/

#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;
int s[MAXN];

int biss(int a){
	return (a%400 == 0 || (a%4 == 0 && a%100 != 0));
}

int main (){
	scanf("%d", &n);
	int a, cnt = 0;
	if(biss(n)){
		for(a=n+1;;a++){
			if(a%400 == 0 || (a%4 == 0 && a%100 != 0))
				cnt+=2;
			else
				cnt++;
			if(cnt != 0 && cnt%7==0 && biss(a))
				break;
		}
	}
	else{
		for(a=n+1;;a++){
			if(biss(a))
				cnt+=2;
			else
				cnt++;
			if(cnt%7 == 0 && cnt != 0)
				break;
		}
	}
	printf("%d\n", a);
}

#include<bits/stdc++.h>
#define int long long
#define mod 1000000007
#define trace(x) cerr<<#x<<": "<<x<<" "<<endl
#define tarce(x) cerr<<#x<<": "<<x<<" "<<endl
#define trace2(x, y) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z) cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define sa(a,n) for(int i =0 ; i<n ; i++) cin>>a[i]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define pb push_back
#define inf 2e18
#define scan() int n; cin>>n ; int a[n]; for(int i = 0 ; i<n ; i++) cin>>a[i]
#define print(a,n) for(int i = 0 ; i < n ; i++) cout<<a[i]<<' '
#define mem(a,v) memset(a,v,sizeof(a))
#define pii pair<int,int>
#define mkp make_pair
using namespace std;
#define N 200005

int32_t main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);

        int y;
        cin >> y;
        int ans,i,j;
        int r=y%4;
        if(r==2 || r==3){
            cout << y+11;
            return 0;
        }
        if(r==1){
            cout << y+6 << '\n';
            return 0;
        }
        if(r==0 && !(y%400==0 || (y%4==0 && y%100!=0))){
            cout << y+28 << '\n';
            return 0;
        }

        if(y%400==0 || (y%4==0 && y%100!=0)){
            cout << y+28;
            return 0;
        }

}

#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define y1 yy
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;

ll n, k, c;

int main () {
    fast_io

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    cin >> n;

    if (n%400 == 0 or (n%4 == 0 and n%100 != 0)) {
       cout << n+28;
       return 0;
    }
    cout << n+6;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
bool lp(int y)
{
if((y%4)==0)
{if((y%100)!=0)
return 1;
else
{  if((y%400)==0)
    return 1;
    else
      {return 0;}
}
}
else
return 0;
}
bool nlc(int y)
{ return ((((y%4)==0) && (y%100)==0) && (y%400)!=0);

 }

int main()
{
int yr;

cin>>yr;
if(lp(yr))
{
if(nlc(yr+28)==0)
yr+=28;
else
yr+=40;
}
else
{
if(nlc(yr+2)|| nlc(yr+3))
yr+=12;
else if(nlc(yr)|| nlc(yr+1))
yr+=6;
else{
if(lp(yr+3) )
yr+=6;
else
yr+=11;
}}
cout<<yr<<'\n';
return 0;
}

/*
ID: nalinpm
PROG: er357p2
LANG: C++
*/

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <fstream>
#include <iomanip>

using namespace std;

int main() {

    unsigned long int n;
    cin >> n;

    unsigned long int ans = n;
    int flagleap = 0;

    if( ((ans % 4 == 0) && (ans%100 != 0))||(ans%400 == 0) )
            flagleap++;

    int count = 0;

    for(int i = 0; i <1000; i++)
    {

        ans++;
        count++;

        if( ((ans % 4 == 0) && (ans%100 != 0))||(ans%400 == 0) )
            count++;

        if(count%7 == 0)
        {
            if(flagleap == 0)
                break;
            else if( ((ans % 4 == 0) && (ans%100 != 100))||(ans%400 == 0))
                break;
        }
    }

    cout << ans<< "\n";

    return(0);
}

#include<bits/stdc++.h>
using namespace std;
#define int long long

signed main()
{
   int y;
   cin >> y;
   if(y%400 == 0 || y%4 == 0)
   {
       cout << y+28 ;
   }
   else
   {
       if(y%4 == 1)
       {
           cout << y+6;
       }
       else
       {
           cout << y+11 << "\n";
       }
   }
}

// http://codeforces.com/contest/678/problem/B
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long int ulli;

int main(int argc, char const *argv[])
{
	ulli n, val, ans = 0;
	cin>>n;

	val = n % 4;

	if(val == 0 || n % 400 == 0)
		cout<<n+28<<'\n';
	if(val == 1)
		cout<<n+6<<'\n';
	if(val == 2 || val == 3)
		cout<<n+11<<'\n';
	return 0;
}

#pragma comment(linker, "/STACK:256000000")
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include <set>
#include <queue>
#include <map>
#include <vector>

using namespace std;
#define mp make_pair
typedef long long ll;
typedef long double ldb;
typedef double db;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const int nmax = 100010;
ll res, cnt = 1e+9 + 7;
int n, m, a, b, l, c, x, y, z, rs;
string s;
vector<pii> wr, dg, sm;
int main()
{
	/*#ifdef _MY_DEBUG
	freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout);
	#else
	#endif*/
	//freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout);
	cin >> n;
	if (n % 400 == 0 || (n % 4 == 0 && n % 100 != 0))
	{
		n += 28;
	}
	if ((n - 1) % 400 == 0 || ((n - 1) % 4 == 0 && (n - 1) % 100 != 0))
		n += 11;
	if ((n - 2) % 400 == 0 || ((n - 2) % 4 == 0 && (n - 2) % 100 != 0))
		n += 11;
	if ((n - 3) % 400 == 0 || ((n - 3) % 4 == 0 && (n - 3) % 100 != 0))
		n += 6;
	cout << n;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

mt19937_64 llrand((int) chrono::steady_clock::now().time_since_epoch().count());

#define st first
#define nd second
#define pb push_back

#ifndef ONLINE_JUDGE
  #define db(x) cerr << #x << " == " << x << endl
  #define dbs(x) cerr << x << endl
  #define _ << ", " <<
#else
  #define db(x) ((void)0)
  #define dbs(x) ((void)0)
#endif

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;

const int INF = 0x3f3f3f3f, MOD = 1e9+7;
const int N = 1e6+5;

vector<int> val;

void build() {
  for(int i = 1; i < N; i++)
    if(i % 400 == 0 or (i % 4 == 0 and i % 100 != 0)) val.pb(i);
}

bool is(int i) {
  return i % 400 == 0 or (i % 4 == 0 and i % 100 != 0);
}

int y;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  build();

  cin >> y;
  int lower = lower_bound(val.begin(), val.end(), y) - val.begin();

  if(!is(y)) {
    int cnt = 1;
    y++;
    if(is(y)) cnt++;
    while(cnt or is(y)) {
      y++, cnt = (cnt + 1) % 7;
      if(is(y)) cnt = (cnt + 1) % 7;
    }
  } else {
    y = val[lower+7];
  }

  cout << y << "\n";

}

#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;

constexpr int N = 3e6 + 5;

int y, val, ans;

int get(int x) {
  return x % 4 == 0 && x % 100 || x % 400 == 0 ? 366 : 365;
}

void Solve() {
  cin >> y;
  val = get(y);
  ans = y + 1;
  while(val % 7 || get(ans) != get(y)) {
    val += (ans % 4 ? 365 : 366);
    val %= 7;
    ans++;
  }
  cout << ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  // int t;
  // cin >> t;
  // while(t--)
  //auto start = chrono::high_resolution_clock::now();
    Solve();
  // auto end = chrono::high_resolution_clock::now();
  // cout << endl << (chrono::duration_cast<chrono::duration<double>>(end - start)).count();
  return 0;
}

#include <cstdio>

using namespace std;

typedef unsigned int uint;

inline bool isLeap( int year )
{
    return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0 );
}

int main()
{
    uint year;
    scanf( "%i", &year );
    if( isLeap( year )  )
    {
        do{
            year += 28;
        }while( !isLeap(year) );
        printf( "%i\n", year );
    }
    else{
        if( isLeap(year+6) )
            printf( "%i\n", year + 11 );
        else
            printf( "%i\n", year + 6 );
    }
    return 0;
}

#include <iostream>

using namespace std;

int main(){

	int y;
	cin >> y;
	int r = y % 4;

	if( r == 0 && (y % 100) != 0) cout << y + 28;
	else if(r == 0 &&  (y % 400) == 0) cout << y + 28;
	else if(r == 0 || r == 1 || r == 2) cout << y + 6;
	else cout << y+11;
	return 0;
}

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
bool leap(int n)
{
	return (n % 400 == 0) || (n % 4 == 0 && n % 100);
}
int main()
{
	int s,n,b;
	cin>>n;
	b = n;
	n++;
	while(true)
	 {
		s++;
		if (leap(n))
		s++;
		if (s % 7 == 0 && leap(b) == leap(n))
		break;
		n++;
	}
	cout<<n<<endl;

	return 0;
}

/* ***********************************************
Author        :Maltub
Email         :xiang578@foxmail.com
Blog          :htttp://www.xiang578.com
************************************************ */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
//#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define pb push_back
using namespace std;
typedef vector<int> VI;
typedef long long ll;
const ll mod=1000000007;
const int N=2048;

int ok(int n) {
	if(n%100==0) {
		if(n%400==0) return 1;
		else return 0;
	} else if(n%4==0) return 1;
	else return 0;
}

int main() {
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int n;
	scanf("%d",&n);
	if(ok(n)) {
		int cnt=0;
		for(int i=1;; i++) {
			if(ok(n+i)) cnt++;
			if(cnt==7) {
				printf("%d\n",n+i);
				return 0;
			}
		}
	}
	else
	{
		int cnt=0;
		for(int i=1;;i++)
		{
			if(ok(n+i)) cnt++;
			if(cnt&&i%6==0)
			{
				printf("%d\n",n+i);
				return 0;
			}
		}
	}
	return 0;
}

#include <iostream>
using namespace std;
int main()
{ int y;
	cin>>y;
	if(y<100000 && y>=1000)
{	if(y%4==0)
	{
		y=y+28;
	}

	else if((y-1)%4==0)
		y=y+6;
	else
	{	y=y+11;
}

	cout<<y;
	return 0;
}}

#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
using namespace std;
typedef double DB;
typedef long long int LL;
typedef long long int ll;
typedef unsigned long long int ULL;
const int INF = 0x3f3f3f3f;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repu(i, a, b) for(int i = (a); i < (b); i++)
#define repf(i, a, b) for(int i = (a); i <= (b); i++)
#define repd(i, a, b) for(int i = (a); i >= (b); i--)
#define MEM(x,i) memset((x),(i),sizeof(x))
const double PI = acos(-1.0);
const double E = exp(1);
const double EPS = 1e-8;
const long long mod=1LL<<32;

int main(void){
	int year;
	scanf("%d",&year);
	if((year%4==0)&&(year%100!=0)||(year%400==0))
printf("%d\n",year+28);
else
printf("%d\n",year+6);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'

int main(){
	ios_base::sync_with_stdio(0);cin.tie(NULL);
	int y;
	cin>>y;
	if(y%4==0){
		if(y%100==0){
			if(y%400==0)
			y+= 28;
			else
			y+= 6;
		}
		else
		y+= 28;
	}
	else
	y+= 6;

	cout<<y<<endl;
}

#include<iostream>
#define N 100001
using namespace std;

int n, a[366], f[N], tmp[366];

bool leap(int x){
    if( x % 4 != 0 ) return 0;
    if( x % 400 == 0 ) return 1;
    if( x % 100 == 0 ) return 0;
    return 1;
}

void eval(int nn){
    int id = 0, now = f[nn], cnt = 0;
    int l = 365 + leap(nn);
    while( cnt < l ){
        tmp[cnt] = now;
        cnt++;
        now = (now + 1) % 7;
    }
}

int main()
{
    // cout << leap(176) << endl;
    // cout << leap(1000) << endl;

    cin >> n;
    f[1000] = 1;
    for(int i = 1001 ; i < N ; i++){
        int now = 365 + leap(i);
        f[i] = (f[i-1] + now) % 7;
    }

    // cout << f[1001] << ' ' << f[2001] << ' ' << f[1233] << ' ' << f[3999] << endl;
    /*
    eval(n);
    for(int i = 0 ; i < 366 ; i++)
        a[i] = tmp[i];
    */

    for(int i = n+1 ; i < N ; i++){
        if( leap(i) != leap(n) ) continue;
        // eval(i);
        // bool f = 1;
        // for(int j = 0 ; j < 366 && f ; j++)
            // if( a[j] != tmp[j] ) f = 0;
        if( f[i] == f[n] ) return cout << i << endl, 0;
    }
}

#include <bits/stdc++.h>

#define rep(i, n) for (long long i=0; i<n; i++)
#define eps 1e-9

using namespace std;

typedef long long ll;

double sqr(double a){
    return a*a;
}
bool doubleEqual(double a, double b){
    return fabs(a-b)<1e-9;
}
bool doubleLessOrEqual(double a, double b){
    return a<b || doubleEqual(a, b);
}
bool doubleLess(double a, double b){
    return a<b && !doubleEqual(a, b);
}
bool doubleGreaterOrEqual(double a, double b){
    return a>b || doubleEqual(a, b);
}
bool doubleGreater(double a, double b){
    return a>b && !doubleEqual(a, b);
}
double mySqrt(double a){
    if (doubleLess(a,0)) throw "sqrt(-1)";
    if (a<0) return 0;
    return sqrt(a);
}

struct Point{
    double x, y;
    Point(): x(0), y(0) {}
    Point(double x, double y): x(x), y(y) {}
    void scan(){
        scanf("%lf %lf", &x, &y);
    }
    void print() const{
        printf("%.10lf %.10lf\n", x, y);
    }
    Point operator+(const Point &p) const {
        return Point(x+p.x, y+p.y);
    }
    Point operator-(const Point &p) const {
        return Point(x-p.x, y-p.y);
    }
    Point operator-() const {
        return Point(-x, -y);
    }
    Point operator*(double k) const {
        return Point(x*k, y*k);
    }
    Point operator/(double k) const {
        return Point(x/k, y/k);
    }
    double operator%(const Point &p) const {
        return x*p.x+y*p.y;
    }
    double operator*(const Point &p) const {
        return x*p.y-y*p.x;
    }
    double length() const {
        return mySqrt(*this % *this);
    }
    double distTo(const Point &p) const {
        return (*this -p).length();
    }
    double distTo(const Point &A, const Point &B) const {
        double d=A.distTo(B);
        if (doubleEqual(d,0)){
            throw "A = B";
        }
        double s = (*this - A) * (*this - B);
        return fabs(s)/d;
    }
    Point normalize(double k=1) const {
        double len = length();
        if(doubleEqual(len, 0)) {
            if (doubleEqual(k, 0)){
                return Point();

            }
            throw "zero-size vector";
        }
        return *this*(k/len);
    }
    Point getH(const Point &A, const Point&B) const {
        Point C = *this;
        Point v = B - A;
        Point u = C - A;
        double k=v%u/v.length();
        v=v.normalize(k);
        Point H=A+v;
        return H;
    }
    Point rotate() const {
        return Point(-y, x);
    }
    Point rotate(double alpha) const {
        return rotate(cos(alpha), sin(alpha));
    }
    Point rotate(double cosa, double sina) const {
        Point v = *this;
        Point u = v.rotate();
        Point w = v*cosa + u*sina;
        return w;
    }
    bool isZero() const{
        return doubleEqual(x, 0) && doubleEqual(y, 0);
    }
    bool isOnline(const Point &A, const Point &B) const {
        return doubleEqual((A-*this)*(B-*this), 0);
    }
    bool isInSegment(const Point &A, const Point &B) const {
        return doubleLessOrEqual((A-*this)%(B-*this), 0);
    }
    bool isInSegmentStricly(const Point &A, const Point &B) const {
        return doubleLess((A-*this)%(B-*this), 0);
    }
    double getAngle() const {
        return atan2(y, x);
    }
    double getAngle(Point u) const {
        return atan2(*this*u, *this%u);
    }
};

int getIntersection(const Point &A, const Point &B, const Point &C, const Point &D, Point &O) {
    Point v = B-A;
    double s1 = (C-A)*(D-A);
    double s2 = (D-B)*(C-B);
    double s = s1+s2;
    if (doubleEqual(s, 0)){
        if (!A.isOnline(C,D))
            return 0;
        return 2;
    }
    v = v/s;
    v = v*s1;
    O = A+v;
    return 1;
}

int getIntersection(const Point &A, double rA, const Point &B, double rB, Point &M, Point &N){
    double d=A.distTo(B);
    if (doubleLess(rA+rB, d) || doubleLess(d, fabs(rA-rB))){
        return 0;
    }
    double a = (sqr(rA)-sqr(rB) + sqr(d))/2/d;
    double h=mySqrt(sqr(rA)-sqr(a));
    Point v = B-A;
    Point u = v.rotate();
    v = v.normalize(a);
    u = u.normalize(h);
    Point H=A+v;
    M=H+u;
    N=H-u;
    if (u.isZero())
        return 1;
    return 2;
}

int getIntersection(const Point &A, const Point &B, const Point &O, double r, Point &M, Point &N){
    double h = O.distTo(A,B);
    if (doubleLess(r, h)){
        return 0;
    }
    Point H=O.getH(A,B);
    Point v=B-A;
    double k=mySqrt(sqr(r)-sqr(h));
    v=v.normalize(k);
    M=H+v;
    N=H-v;
    if(v.isZero())
        return 1;
    return 2;
}

struct cmp{
    bool operator()(const string &a, const string &b){
        return a+b<b+a;
    }
};

ll GCD(ll a, ll b){
    return (!b?a:GCD(b, a%b));
}

ll POW(ll a, ll b){
    return (!b?1:b==1?a:b%2?POW(a,b>>1)*POW(a,b>>1)*a:POW(a,b>>1)*POW(a,b>>1));
}

int main(){
    ifstream fin("file.in");
    ofstream fout("file.out");
    cin.sync_with_stdio(false);
    cout.sync_with_stdio(false);
    fin.sync_with_stdio(false);
    fout.sync_with_stdio(false);

    ll n, k, x, m, p, q, a, b, res=0;
    cin >> n;

    n = ((((n%4)==0) && ((n%100)!=0) || ((n%400)==0))?n+28:n+6);
    cout << ((((n%4)==0) && ((n%100)!=0) || ((n%400)==0))?n+6:n+0);

    fin.close();
    fout.close();
    return 0;
}

#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <map>

using namespace std;
//ifstream cin("input.txt"); ofstream cout("output.txt");
int main() {
	int n;
	cin >> n;
	int i = n + 1;
	while (n % 7 != i % 7) {
		if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
			i += 2;
		}
		else {
			i += 1;
		}
	}
	cout << i;
	return 0;
}

#include<iostream>
#include<cmath>
using namespace std;
int leap_Year(int x)
{
	if(x%4!=0) return 0;
	else
	{
		if(x%100!=0) return 1;
		else
		{
			if(x%400!=0) return 0;
			else return 1;
		}
	}
}
main()
{
	int x;
	cin>>x;
	if(leap_Year(x)==1)
	{
		if(leap_Year(x+28)==0)
		{
			cout<<x+28+12;
		}
		else cout<<x+28;
	}
	else
	{
		if(leap_Year(x+1)==1)
		{
			cout<<x+11;
		}
		else
		cout<<x+6;
	}
}

#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

#define ll long long
#define ull unsigned long long
#define pb push_back
#define sz size()
#define sqr(x) ((x) * (x))
#define mp make_pair
#define F first
#define S second
#define all(c) c.begin(), c.end()
#define tr(container, it) \
for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define present(container, element) (container.find(element) != container.end()) // set, map
#define cpresent(container, element) (find(all(container), element) != container.end())
bool check(int n) {
	if(n % 400 == 0) return true;
	if(n % 4 == 0 && n % 100 != 0) return true;
	return false;
}
int day(int n) {
	int cnt = 5;
	if(n > 2016) {
		for(int i = 2017; i <= n; i++) {
			if(check(i - 1)) cnt = (cnt + 2) % 7;
			else cnt = (cnt + 1) % 7;
		}
		return cnt;
	}
	else {
		for(int i = 2015; i >= n; i--) {
			if(check(i) == 0) cnt = (cnt - 1) % 7;
			else cnt = (cnt - 2) % 7;
			if(cnt < 0) cnt += 7;
		}
		return cnt;
	}
}
int main(int argc, char* argv[]) {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	#ifndef ONLINE_JUDGE
  	freopen("in", "r", stdin); freopen("out", "w", stdout);
  	#endif
	int n, cnt = 5, cnt2 = 0;
	cin >> n;
	for(int i = n + 1; i < 1e5; i++) {
		if(day(i) == day(n) && check(i) == check(n)) {
			cout << i << endl;
			return 0;
		}

	}
	#ifndef ONLINE_JUDGE
	cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s";
	#endif
	return 0;
}

#include <iostream>
#include <cctype>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>

using namespace std;

/*int lastOne(int A[], int item, int start, int end) {
	int mid;
	while (start < end) {
		mid = (start + end) / 2;

		if (item >= A[mid]) {
			end = mid;
		} else {
			start = mid + 1;
		}
	}
	return end;

}*/

int main() {

	 int year ;
	 cin >> year ;
	 int out = 0 ;
	 if( year%4==0 || year%400==0 ){
		 out = year + 28 ;
	 }
	 else{
		 out = year + 6 ;
	 }
	 cout << out << endl;

	return 0;
}

#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
using namespace std;
typedef double DB;
typedef long long int LL;
typedef long long int ll;
typedef unsigned long long int ULL;
const int INF = 0x3f3f3f3f;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repu(i, a, b) for(int i = (a); i < (b); i++)
#define repf(i, a, b) for(int i = (a); i <= (b); i++)
#define repd(i, a, b) for(int i = (a); i >= (b); i--)
#define MEM(x,i) memset((x),(i),sizeof(x))
const double PI = acos(-1.0);
const double E = exp(1);
const double EPS = 1e-8;
const long long mod=1LL<<32;

int main(void){
	int year;
	scanf("%d",&year);
	int dd;
	int qq;
	if((year%4==0)&&(year%100!=0)||(year%400==0)){
	qq=year+28;
	printf("%d\n",qq);
}
else{
	int sd=0;
	for(int i=year-1;;i++){
	if((year%4==0)&&(year%100!=0)||(year%400==0)){
		sd+=2;
		dd=366;
	}else{
	 sd+=1; dd=365;}
	if(sd%7==0&&dd==365){
		printf("%d\n",i+1);
		break;
	}
	}
}

	return 0;
}

#include<bits/stdc++.h>
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef vector<ll> vi;

#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define nl cout<<'\n'
#define pb push_back
#define all(s) s.begin(),s.end()
#define bs binary_search

#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) (a)*(b)/(__gcd((a),(b)))

const int Mod=1e9+7;
#define sum(a,b) (((a)%Mod)+((b)%Mod))%Mod
#define sub(a,b) (((a)%Mod)-((b)%Mod))%Mod
#define mul(a,b) (((a)%Mod)*((b)%Mod))%Mod
#define digit(x) floor((log10(x))+1)

#define count1(x) __builtin_popcount(x)

#define dbg(x) cout << #x <<":"<<x<<endl;

bool isPowerOfTwo (int x)
{
    /* First x in the below expression is for the case when x is 0 */
    return x && (!(x&(x-1)));
}

ll pow(ll x, ll y, ll mod)
{
	int ans=1;
	while(y)
	{
		if(y&1)
			ans=(ans*x)%mod;
		y/=2;
		x=(x*x)%mod;
	}
	return ans;
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
        freopen("debug.txt","w",stderr);
	#endif
	fast;
	ll n;
	cin >> n;
	if(n%4==0)
	{
		n+=28;
	}
	else if(n%4==1)
	{
		n+=6;
	}
	else
	{
		n+=11;
	}
	cout << n << endl;
}

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn = 1000100;
LL n, h, k;
LL a[maxn];

int main(){
	while(~scanf("%lld",&n)){
		if(n%400==0||(n%4==0&&n%100)){
			printf("%lld\n",n+28);
		}
		else{
			printf("%lld\n",n+6);
		}

	}
    return 0;
}

// MOTIVATION IS A FIRE FROM WITHIN

#define pb push_back
#define mp make_pair
#define pp push
#define f first
#define s second

typedef long long lld;
typedef long double ldb;
typedef unsigned long long int ulld;

#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007

main(){
	lld y;
	cin >> y;
	bool leap = 0;
	if(y%4==0 && y%100!=0){
		leap = 1;
	}
	if(y%400==0){
		leap = 1;
	}
	lld z = y, cnt;
	lld rem = z%4;
	if(rem==0){
		cnt = 2;
	}
	else cnt = 1;

	for(lld i=rem+1; ; i++){

		if(cnt%7==0){
			if(leap){
				if((z - rem + i)%400==0 || ((z - rem + i)%4==0 && (z - rem + i)%100!=0 )){
					cout << z - rem + i << endl;
					return 0;
				}
			}
			else{
				if(!((z - rem + i)%400==0 || ((z - rem + i)%4==0 && (z - rem + i)%100!=0 ))){
					cout << z - rem + i << endl;
					return 0;
				}
			}
		}
				if(i%4==0) cnt+=2;
		else cnt++;
	}
}

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int y;
	cin>>y;
	int f=0;
	if(y%400==0)
	f=1;
	else if(y%4==0 && y%100!=0)
	f=1;
	if(f==1)
	y=y+28;
	else
	y=y+6;
	cout<<y<<endl;
	return 0;
}

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int test(int x);
int main()
{
    int y,num=test(y)+1,i;
    scanf("%d",&y);
    for(i=y+1;num||test(y)!=test(i);i++)
    {
        if(test(i))
            num+=2;
        else
            num++;
        num%=7;
    }
    printf("%d",i);
}

int test(int x)
{
    if(x%400==0||(x%4==0&&x%100!=0))
        return 1;
    else
        return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int y;
	cin>>y;
	int f=0;
	if(y%400==0)
	f=1;
	else if(y%4==0 && y%100!=0)
	f=1;
	if(f==1)
	y=y+28;
	else
{	y=y+6;
  if(y%400==0||(y%4==0 && y%100!=0))
  y=y+5;
}
	cout<<y<<endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;
int s[MAXN];

int main (){
	scanf("%d", &n);
	int a, cnt = 0;
	if(n%400 == 0 || (n%4 == 0 && n%100 != 0)){
		for(a=n+1;cnt < 7;a++){
			if(a%400 == 0 || (a%4 == 0 && a%100 != 0))
				cnt++;
		}
		a--;
	}
	else{
		a = n+6;
	}
	printf("%d\n", a);
}

#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;
int s[MAXN];

int main (){
	scanf("%d", &n);
	int a, cnt = 0;
	if(n%400 == 0 || (n%4 == 0 && n%100 != 0)){
		for(a=n+1;cnt < 7;a++){
			if(a%400 == 0 || (a%4 == 0 && a%100 != 0))
				cnt++;
		}
		a--;
	}
	else{
		for(a=n+1;cnt==0 || cnt%7!=0;a++){
			if(a%400 == 0 || (a%4 == 0 && a%100 != 0))
				cnt+=2;
			else
				cnt++;
		}
		a--;
	}
	printf("%d\n", a);
}

#include <iostream>

using namespace std;

bool is_leap(int x){
    if(x%4==0){
        if(x%100==0&&x%400!=0)
            return 0;
        else
            return 1;
    }else return 0;
}
int main()
{
    int year;
    while(cin>>year){
        int temp=year;
        int day=1;
        if(is_leap(year)){
            if(is_leap(year+28))
                cout<<year+28<<endl;
            else{
                cout<<year+40<<endl;
            }
        }else{
            while(1){
                temp++;
                if(is_leap(temp))
                    day+=2;
                else
                    day++;
                if(day%7==0){
                    cout<<temp+1<<endl;
                    break;
                }
            }
        }
    }
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    bool leap;
    if(n%400==0)
        leap=true;
    else if(n%4==0 && n%100==0)
        leap=false;
    else if(n%4==0)
        leap=true;
    else
        leap=false;
    if(leap)
        cout<<n+28;
    else
        cout<<n+6;
return 0;
}

#include<bits/stdc++.h>
using namespace std;
bool isLeap(int n)
{
    bool leap;
    if(n%400==0)
        leap=true;
    else if(n%4==0 && n%100==0)
        leap=false;
    else if(n%4==0)
        leap=true;
    else
        leap=false;
    return leap;
}
int main()
{
    int n;
    cin>>n;
    if(isLeap(n))
    {
        if(isLeap(n+4))
            cout<<n+28;
        else
            cout<<n+12;
    }
    else if(isLeap(n+1))
    {
        if(isLeap(n+5))
            cout<<n+11;
        else
            cout<<n+6;
    }
    else if(isLeap(n+2))
    {
        if(isLeap(n+6))
            cout<<n+11;
        else
            cout<<n+6;
    }
    else if(!isLeap(n+3))
    {
        if(isLeap(n+5)||isLeap(n+4))
            cout<<n+6;
        else
            cout<<n+12;
    }
    else
        cout<<n+6;
return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long

int32_t main() {
    IOS;
    int n;
    cin>>n;
    bool flag=false;
    if(n%400==0 || (n%4==0 && n%100!=0)) flag=true;
    if(flag) cout<<n+28<<endl;
    else cout<<n+6<<endl;
	return 0;
}

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <iomanip>
#include <cmath>
#include <vector>
#include<cctype>
#include<algorithm>
#include<set>
#include<stack>
#include<map>
#include<stdlib.h>
#include<ctime>
#include<iterator>
#include <deque>
#include <sstream>
#include<queue>
#include<cstdlib>
#include<climits>

#define ll long long
#define fn(i,n) for(int (i) = 0;(i) < (n);(i)++)
#define fs(i) for(int (i) = 1;(i) <= 9;(i)++)

using namespace std;

const string days[7] = {"mon","tus","wed","thu","fri","sut","sun"};

int main(){
	int n,idx = 0;
	cin>>n;
	int mod = n % 4;
	for(int i = n+1;;i++){
		if(i % 4 == 0){
			if(idx == 6)
				idx = 1;
			else if(idx == 5)
				idx = 0;
			else idx += 2;
		}else if(idx == 6)
			idx = 0;
		else idx++;
	//	cout<<i<<" "<<days[idx]<<endl;
		if((i % 4 == mod|| i % 4 == 4 - mod) && idx == 0){
			cout<<i<<endl;
			return 0;
		}
	}

	return 0;
}