#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e7;
bool prime[MAXN + 5];
int rev(int x) {
  int ans = 0;
  while (x) {
    ans = ans * 10 + x % 10;
    x /= 10;
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  for (int i = 2; i <= MAXN; i++) {
    prime[i] = true;
  }
  for (int i = 2; i * i <= MAXN; i++) {
    for (int j = i; i * j <= MAXN; j++) {
      prime[i * j] = false;
    }
  }
  int cnt = 0;
  for (int i = 1;; i++) {
    if (i != rev(i) && prime[i] && prime[rev(i)]) {
      cnt++;
      if (cnt == n) {
        cout << i;
        return 0;
      }
    }
  }
  return 0;
}
