// Author: Michael Mattes
#include<iostream>
#include<fstream>
#include<sstream>
#include<set>

using namespace std;

string pad[9] = {
	"00000000",
	"0000000",
	"000000",
	"00000",
	"0000",
	"000",
	"00",
	"0",
	""
};


long next(long a) {
//	cout << "next: " << a;
	ostringstream t;
	t << (a*a);
	string s = t.str();
//	cout << " " << s;
	s = pad[s.length()] + s;
//	cout << " " << s.substr(2,4);
	stringstream sin(s.substr(2,4));
	long ret;
	sin >> ret;
//	cout << " " << ret << endl;
	return ret;
}

long count(long a) {
	set<long> s;
	while(s.count(a)==0) {
		s.insert(a);
		a = next(a);
	}
	return(s.size());
}

#define fin cin

int main() {
//	fstream fin("generate.in");
	while(1) {
		long a;
		fin >> a;
		if(a == 0) break;
		cout << count(a) << endl;
	}
	return 0;
}
