#include <bits/stdc++.h>
using namespace std;
bool ss[1000005];
int a[100005];
void init() {
  ss[1] = 0;
  for (int i = 2; i <= 1000000; i++) {
    if (!ss[i])
      for (int j = 2; j * i <= 1000000; j++) ss[i * j] = 1;
  }
}
int reverse(int x) {
  int res = 0, t = x;
  while (x > 0) {
    res = res * 10 + x % 10;
    x /= 10;
  }
  if (res != t)
    return res;
  else
    return 6;
}
signed main() {
  init();
  int n;
  cin >> n;
  int l = 0, pos = 1;
  while (l < n) {
    pos++;
    if (!ss[pos] && !ss[reverse(pos)]) l++;
  }
  cout << pos << endl;
  return 0;
}
