#include <bits/stdc++.h>
using namespace std;
template <class T1>
inline void debug(T1 _x) {
  cout << _x << '\n';
}
template <class T1, class T2>
inline void debug(T1 _x, T2 _y) {
  cout << _x << ' ' << _y << '\n';
}
template <class T1, class T2, class T3>
inline void debug(T1 _x, T2 _y, T3 _z) {
  cout << _x << ' ' << _y << ' ' << _z << '\n';
}
template <class T1, class T2, class T3, class T4>
inline void debug(T1 _x, T2 _y, T3 _z, T4 _zz) {
  cout << _x << ' ' << _y << ' ' << _z << ' ' << _zz << '\n';
}
template <class T1>
inline void debug(T1 _array, int _size) {
  cout << "[";
  for (int i = 0; i < _size; ++i) {
    cout << ' ' << _array[i];
  }
  puts(" ]");
}
inline bool CI(int &_x) { return scanf("%d", &_x) == 1; }
inline bool CI(int &_x, int &_y) { return CI(_x) && CI(_y); }
inline bool CI(int &_x, int &_y, int &_z) { return CI(_x) && CI(_y) && CI(_z); }
inline bool CI(int &_x, int &_y, int &_z, int &_zz) {
  return CI(_x) && CI(_y) && CI(_z) && CI(_zz);
}
inline void wait(double seconds) {
  double endtime = clock() + (seconds * CLOCKS_PER_SEC);
  while (clock() < endtime) {
    ;
  }
}
const int inf = (int)1000000;
bitset<inf> used;
int primes[inf];
int cnt;
inline bool isP(int x) {
  if (x < 2) return false;
  if (x == 2) return true;
  if (x % 2 == 0) return false;
  return used[x] == false;
}
inline void sieve() {
  int sqrtN = (int)sqrt((double)inf) + 1;
  used = 0;
  int i, j;
  for (i = 3; i <= sqrtN; i += 2) {
    if (!used[i]) {
      for (j = i * i; j < inf; j += i) {
        used[j] = 1;
      }
    }
  }
  cnt = 0;
  primes[cnt++] = 2;
  for (i = 3; i < inf; i += 2) {
    if (!used[i]) primes[cnt++] = i;
  }
}
inline int toI(string _str) {
  assert((int)(_str).length());
  int var;
  sscanf(_str.c_str(), "%d", &var);
  return var;
}
inline string toS(int var) {
  char _buff[50];
  sprintf(_buff, "%d", var);
  return (string)_buff;
}
inline void Proc() {
  sieve();
  int x;
  CI(x);
  for (int i = 0, j1 = cnt; i < j1; ++i) {
    int var = primes[i];
    string rvar = toS(var);
    reverse(rvar.begin(), rvar.end());
    int rr = toI(rvar);
    if (isP(rr) && rr != var) {
      --x;
    }
    if (x == 0) {
      cout << var;
      puts("");
      return;
    }
  }
}
int main() {
  int tt = 1;
  for (int i = 1, j1 = tt + 1; i < j1; ++i) {
    Proc();
  }
  return 0;
}
