s = 0
unseen = 1
seen(v) = bit test(s, v)
see(v) = s = bit or(s, 2^v); while (seen(unseen), unseen++)

other(p) = {
	see(p);
	my (m=digits(p)[1]);
	forstep (v=ceil(unseen/m)*m, oo, m,
		if (!seen(v),
			return (v);
		);
	);
}

b = vector(10 000)
u = 1
{
	v=1; for (n=1, oo, 
		if (v<=#b,
			b[v] = n;
			while (b[u],
				print (u " " b[u]);
				if (u++>#b,
					break (2);
				);
			);
		);
		v = other(v);
	);
}

quit