#include <bits/stdc++.h>
using namespace std;
int n;
const int N = 1e3 + 20;
int reverser(int n) {
  int counter = 0;
  int last = 0;
  int ans = 0;
  while (n > 0) {
    last = n % 10;
    ans = (ans * 10) + last;
    n /= 10;
  }
  return ans;
}
bool is_prime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0) return false;
  return (n > 1);
}
void input() { cin >> n; }
void solve() {
  int counter2 = 0;
  int reverse, ans, ans1;
  for (int counter1 = 11; counter1 < 1000000; counter1++) {
    reverse = reverser(counter1);
    if (is_prime(counter1) && is_prime(reverse) && reverse != counter1) {
      counter2++;
      ans = counter1;
    }
    if (counter2 == n) {
      break;
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(), cout.tie();
  input();
  solve();
  return 0;
}
