#include <bits/stdc++.h>
using namespace std;
const int MAX = 200 * 11184;
int isnotprime[MAX];
int rev(int x) {
  int y = 0;
  while (x > 0) {
    y *= 10;
    y += x % 10;
    x /= 10;
  }
  return y;
}
int solve(int n) {
  memset(isnotprime, 0, sizeof(isnotprime));
  int k = 1;
  for (int x = 2; x < MAX; x++) {
    if (!isnotprime[x]) {
      for (int y = x + x; y < MAX; y += x) {
        isnotprime[y] = true;
      }
      if (x >= 13) {
      }
    }
  }
  for (int x = 10; x < MAX; x++) {
    if (!isnotprime[x]) {
      int y = rev(x);
      if ((x != y) && (y < MAX) && !isnotprime[y]) {
        if (k == n) {
          return x;
        }
        k++;
      }
    }
  }
  return -1;
}
inline void init() {}
int main() {
  init();
  bool prev = false;
  int x;
  while (cin >> x) {
    if (prev) {
      cout << endl;
    }
    prev = true;
    cout << solve(x) << endl;
  }
  return 0;
}
