#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[10001] = {0}, b[5000] = {0}, c = 0, i, j;
  for (i = 2; i < 10001; i++) {
    if (!a[i])
      for (j = i + i; j < 10001; j += i) a[j] = 1;
  }
  for (i = 2; i < 10001; i++)
    if (!a[i])
      if (!a[i + 2])
        if (!a[i + 6])
          if (!a[i + 8]) b[c++] = a[i + 8];
  for (c = i = 0; i < 10001; i++) {
    if (i >= b[c + 1]) c++;
    a[i] = b[c];
  }
  while (cin >> c, c) cout << a[c] << endl;
}
