#include <bits/stdc++.h>
using namespace std;
const int N = 1000001;
bool notp[N];
void sieve() {
  notp[0] = notp[1] = 1;
  for (int i = 2; i < N; ++i) {
    if (notp[i]) continue;
    for (int j = i * 2; j < N; j += i) notp[j] = 1;
  }
}
int rev(int v) {
  int ans = 0;
  while (v) {
    ans *= 10;
    ans += (v % 10);
    v /= 10;
  }
  return ans;
}
int main() {
  sieve();
  int n;
  scanf("%d", &n);
  int c = 0;
  for (int i = 1; i < N; ++i) {
    if (!notp[i] && (!notp[rev(i)] && i != rev(i))) {
      ++c;
    }
    if (c == n) {
      printf("%d", i);
      return 0;
    }
  }
}
