#include <bits/stdc++.h>
using namespace std;
int n;
int out = 0;
int rev(int x) {
  int ret = 0;
  while (x > 0) {
    ret *= 10;
    ret += x % 10;
    x /= 10;
  }
  return ret;
}
bool prime(int x) {
  for (int i = 2; i * i <= x; i++)
    if (x % i == 0) return 0;
  return x > 1;
}
void read() { cin >> n; }
void solve() {
  for (int i = 1, count = 0; count < n; i++) {
    int tmp = rev(i);
    if (i != tmp && prime(i) && prime(tmp)) {
      count++;
      out = i;
    }
  }
}
void write() { cout << out; }
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  read(), solve(), write();
  return 0;
}
