#include<bits/stdc++.h>
using namespace std;
int sosu[1000005]={0};
main(){
  int n;
  sosu[2]=0;
  sosu[1]=1;
  sosu[0]=1;
  for(int i=3;i<=sqrt(1000000);i+=2){
    if(sosu[i]==0){
      for(int j=i*2;j<=1000000;j+=i){
	sosu[j]=1;
      }
    }
  }
  while(cin>>n,n){
    int l,r;
    l=3;
    r=n-3;
    int cnt=0;
    while(l<=r){
      //cout<<l<<" "<<r<<endl;
      if(!sosu[l]&&!sosu[r]){
	//cout<<"A"<<l<<" "<<r<<endl;
	cnt++;
      }
      r-=2;
      l+=2;
    }
    cout<<cnt<<endl;
  }
}