#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 * 10000 + 20;
bool mrk[MAXN];
vector<int> prm;
void prime() {
  int k = 10000001;
  prm.push_back(1);
  mrk[1] = 1;
  for (int i = 2; i < k; i++)
    if (!mrk[i]) {
      prm.push_back(i);
      int cnt = 2;
      while (cnt * i <= k) mrk[cnt * i] = 1, cnt++;
    }
}
long long rev(long long x) {
  long long ret = 0;
  while (x) {
    ret = ret * 10 + x % 10;
    x /= 10;
  }
  return ret;
}
int main() {
  ios ::sync_with_stdio(0);
  int n, cnt = 0, ap;
  cin >> n;
  prime();
  for (int i = 0; i < prm.size(); i++) {
    if (cnt == n) {
      ap = i - 1;
      break;
    }
    if (!mrk[rev(prm[i])] && prm[i] != rev(prm[i])) {
      cnt++;
    }
  }
  cout << prm[ap] << endl;
  return 0;
}
