#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int r = 13, cnt = 0;
  while (true) {
    bool is = true;
    for (int i = 2; i * i <= r; i++)
      if (r % i == 0) {
        is = false;
        break;
      }
    if (!is) {
      r++;
      continue;
    }
    stringstream ss;
    ss << r;
    string t;
    ss >> t;
    reverse(t.begin(), t.end());
    stringstream sp(t);
    int b;
    sp >> b;
    is = true;
    if (b == r) is = false;
    for (int i = 2; i * i <= b; i++)
      if (b % i == 0) {
        is = false;
        continue;
      }
    if (!is) {
      r++;
      continue;
    }
    cnt++;
    if (cnt == n) {
      cout << r << endl;
      return 0;
    } else
      r++;
  }
}
