#include <bits/stdc++.h>
using namespace std;
const int mx = 2e5 + 10;
int ar[mx];
vector<int> vec[7][2];
int leap(int m) {
  if (m % 400 == 0)
    return 1;
  else if (m % 100 and m % 4 == 0)
    return 1;
  return 0;
}
int main() {
  ar[1000] = 1;
  vec[ar[1000]][leap(1000)].push_back(1000);
  for (int i = 1001; i < mx; i++) {
    ar[i] = (ar[i - 1] + 365 + leap(i - 1)) % 7;
    vec[ar[i]][leap(i)].push_back(i);
  }
  int n;
  while (cin >> n) {
    int l =
        *upper_bound(vec[ar[n]][leap(n)].begin(), vec[ar[n]][leap(n)].end(), n);
    cout << l << "\n";
  }
  return 0;
}
