#include <bits/stdc++.h>
long rev(long n) {
  long out = 0;
  while (n > 0) {
    out = out * 10 + n % 10;
    n /= 10;
  }
  return out;
}
int main() {
  long n, t = 0, r, f = 12, l = 1000000;
  bool a[l];
  scanf("%ld", &n);
  a[0] = 0;
  a[1] = 0;
  for (long i = 2; i < l; i++) a[i] = 1;
  for (long i = 2; i < l; i++)
    if (a[i]) {
      for (long long j = (long long)i * i; j < l; j += i) a[j] = 0;
    }
  for (; f < l; f++) {
    r = rev(f);
    if (r != f && (a[f] & a[r])) {
      t++;
      if (t == n) break;
    }
  }
  printf("%ld", f);
}
