#include <bits/stdc++.h>
using namespace std;
int main() {

	bool table[1000001];
	fill((bool*) table, (bool*) table + 1000001, true);

	table[0] = false;
	table[1] = false;
	for (int i = 2; i * i <= 1000000; i++) {
		if (table[i]) {
			for (int j = i + i; j <= 1000000; j += i) {
				table[j] = false;
			}
		}
	}

	int n;
	while (~scanf("%d", &n)) {
		if (n == 0)
			break;

		int res = 0;
		for (int i = n, j = 0; j <= n / 2; j++, i--) {
			if (table[i] && table[j])
				res++;
		}

		printf("%d\n", res);
	}
}