#include <bits/stdc++.h>
using namespace std;
const int LIM = 1000009;
int m, n, f;
int us[LIM];
string str(int n) {
  string s = "";
  while (n > 0) {
    s = char(n % 10 + 48) + s;
    n /= 10;
  }
  return s;
}
int val(string s) {
  int res = 0;
  for (int i = 0; i < s.size(); i++) res = res * 10 + (s[i] - '0');
  return res;
}
int main() {
  memset(us, 0, sizeof(us));
  for (int i = 2; i < 1e6; i++)
    if (us[i] == 0) {
      us[i] = 1;
      for (int j = 2; j * i < 1e6; j++) us[j * i] = -1;
    }
  cin >> n;
  f = 15;
  if (n == 1)
    cout << "13\n";
  else {
    m = 1;
    while (1 == 1) {
      string ss = str(f);
      string s2 = ss;
      reverse((s2).begin(), (s2).end());
      if (us[f] == 1 && us[val(s2)] == 1 && ss != s2) m++;
      if (m == n) break;
      if (f % 10 == 3)
        f += 4;
      else
        f += 2;
      if ((ss[0] - '0') % 2 == 0) {
        string ff = ss;
        ff[0]++;
        f = val(ff);
      }
    }
    cout << f << "\n";
  }
}
