#include <bits/stdc++.h>
using namespace std;
string s;
long long i, j, k, l;
long long a[1000005] = {0};
void seive() {
  a[0] = 1;
  a[1] = 1;
  for (i = 2; i < 1000002; i++) {
    if (a[i] == 0) {
      for (j = 2 * i; j < 1000002; j += i) {
        a[j] = 1;
      }
    }
  }
}
long long rev(long long num) {
  long long pp = 0;
  while (num > 0) {
    pp = pp * 10 + (num % 10);
    num /= 10;
  }
  return pp;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  long long n, ans;
  seive();
  ans = 0;
  {
    i = 0;
    cin >> n;
    for (i = 13; ans < n; i++) {
      if (a[i] == 0 && a[rev(i)] == 0 && rev(i) != i) {
        ans++;
      }
      if (ans == n) break;
    }
    cout << i;
  }
  return 0;
}
