/*
TASK: crazy
LANG: C++
*/
#include <algorithm>
#include <climits>
#include <cstdio>
#include "module.h"

using namespace std;

const int SIZE = 1000;
const int MAXP = 1000;

long X, Y;
long X1, Y1;
bool t[SIZE + 1][SIZE + 1];
int p[SIZE + 1][SIZE + 1];
short hod_a[SIZE + 1][SIZE + 1];
short hod_b[SIZE + 1][SIZE + 1];
int g[SIZE + 1][SIZE + 1];
bool computed_t[SIZE + 1][SIZE + 1];
//int maxN;
bool prosto[MAXP + 1];

/*
bool eProsto(int x) {
	if (x % 2 == 0) return false;
	int sqr = x * x;
	for (int i = 3; i <= sqr; i += 2) {
		if (x % i == 0) return false;
	}
	return true;
}
*/

inline bool eProsto(int x) {
	return prosto[x];
}

int nod(int a, int b) {
	if (a < b) {
		swap(a, b);
	}
	while (b > 0) {
		int r = a % b;
		a = b;
		b = r;
	}
	return a;
}

bool tip(int x, int y) {
//	if (x > maxN) maxN = x;
//	if (y > maxN) maxN = y;
	if (x > y) return tip(y, x);
	if (computed_t[x][y]) return t[x][y];
	if (eProsto(x) && eProsto(y)) {
		t[x][y] = false;
		g[x][y] = 1;
		return t[x][y];
	}
	int minP = INT_MAX;
	int maxG = 0;
	int hodap = -1, hodbp = -1;
	int hodag = -1, hodbg = -1;
	
	bool imaP = false;
	//zamestvame pyrvoto chislo - x
	for (int i = 2; i < x; i++) {
		if (nod(i, x) != 1) {
			if (tip(i, y + 1)) {
				if (p[i][y + 1] + 1 > maxG) {
					maxG = p[i][y + 1] + 1;
					hodag = i;
					hodbg = y + 1;
				}
			} else {
				imaP = true;
				if (g[i][y + 1] + 1 < minP) {
					minP = g[i][y + 1] + 1;
					hodap = i;
					hodbp = y + 1;
				}
			}
			
		}
	}
	//zamestvame vtoroto chislo - y
	for (int i = 2; i < y; i++) {
		if (nod(i, y) != 1) {
			if (tip(i, x + 1)) {
				if (p[i][y + 1] + 1 > maxG) {
					maxG = p[i][y + 1] + 1;
					hodag = x + 1;
					hodbg = i;
				}
			} else {
				imaP = true;
				if (g[i][y + 1] + 1 < minP) {
					minP = g[i][y + 1] + 1;
					hodap = x + 1;
					hodbp = i;
				}
			}
		}
	}
	if (imaP) {
		t[x][y] = true;
		p[x][y] = minP;
		p[y][x] = minP;
		hod_a[x][y] = hodap;
		hod_b[x][y] = hodbp;
		computed_t[x][y] = true;
		return t[x][y];
	} else {
		t[x][y] = false;
		g[x][y] = maxG;
		g[y][x] = maxG;
		hod_a[x][y] = hodag;
		hod_b[x][y] = hodbg;
		computed_t[x][y] = true;
		return t[x][y];
	}
}

void hod() {
	bool sw = X > Y;
	if (sw) {
		swap(X, Y);
	}
	X1 = hod_a[X][Y];
	Y1 = hod_b[X][Y];
	if (sw) {
		swap(X1, Y1);
	}
}

/*
void getnum(long* X, long* Y) {
	scanf("%ld%ld", X, Y);
}

void setnum(long X1, long Y1) {
	printf("%ld %ld\n", X1, Y1);
}
*/

void eratosten() {
	for (int i = 2; i <= MAXP; i++) {
		prosto[i] = true;
	}
	int p = 2;
	for ( ; ; ) {
		if (p > MAXP) break;
		while (!prosto[p]) p++;
		for (int i = p; i <= MAXP; i += p) {
			prosto[i] = false;
		}
		p++;
        }
}

int main() {
	for (int i = 0; i <= SIZE; i++) {
		for (int j = 0; j <= SIZE; j++) {
			computed_t[i][j] = false;
		}
	}
	eratosten();
//	maxN = 0;
	for ( ; ; ) {
		getnum(&X, &Y);
		//if (X == 0 && Y == 0) break;
		//printf("X=%ld Y=%ld\n", X, Y);
		tip(X, Y);
//		printf("maxN=%d\n", maxN);
		hod();
		/*
		printf("tip:\n");
		for (int i = 1; i <= 10; i++) {
			for (int j = 1; j <= 10; j++) {
				printf(" %d", (t[i][j]) ? 1 : 0);
			}
			printf("\n");
		}
		printf("p:\n");
		for (int i = 1; i <= 10; i++) {
			for (int j = 1; j <= 10; j++) {
				printf(" %d", p[i][j]);
			}
			printf("\n");
		}
		printf("g:\n");
		for (int i = 1; i <= 10; i++) {
			for (int j = 1; j <= 10; j++) {
				printf(" %d", g[i][j]);
			}
			printf("\n");
		}
		*/
		//printf("X1=%ld Y1=%ld\n", X1, Y1);
		setnum(X1, Y1);
	}
}

