#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

int gcd(int a, int b)
{
	while(a)
		swap(a, b%=a);
	return b;
}

class FindPolygons { public:
	double minimumPolygon(int L)
	{
		if(L<=2)
			return -1;
		if(L%2==1) // euclid%2 == manhattan%2. tour is always even.
			return -1;

		map<int, vector<pair<int,int>>> pyt;
		for(int k=1; k<=L; ++k) {
			auto& pk = pyt[k];
			pk.emplace_back(0, k);
			pk.emplace_back(k, 0);
			pk.emplace_back(0, -k);
			pk.emplace_back(-k, 0);
		}
		for(int m=1; m*m<=L/2; ++m)
			for(int n=1; n<m && m*m+n*n<=L/2; ++n)
				if(((m^n)&1) && gcd(m,n)==1) {
					int a = m*m-n*n, b = 2*m*n, c = m*m+n*n;
					for(int k=1; k*c<=L; ++k) {
						auto& pk = pyt[k*c];
						pk.emplace_back(k*a, k*b);
						pk.emplace_back(k*a, -k*b);
						pk.emplace_back(-k*a, k*b);
						pk.emplace_back(-k*a, -k*b);
						pk.emplace_back(k*b, k*a);
						pk.emplace_back(k*b, -k*a);
						pk.emplace_back(-k*b, k*a);
						pk.emplace_back(-k*b, -k*a);
					}
				}

		// Triangle
		int best = L;
		for(int a=1; a*3<=L; ++a)
			for(int b=a; a+b*2<=L; ++b)
			{
				int c = L-a-b;

				for(auto& av : pyt[a])
				{
					int ax=av.first;
					int ay=av.second;
					if(ax>=0 && ay>=0)
					for(auto& bv : pyt[b])
					{
						int bx=bv.first;
						int by=bv.second;
						int cx=-ax-bx;
						int cy=-ay-by;
						if(ax*by!=ay*bx && cx*cx+cy*cy == c*c) {
							best = min(best, c-a);
							break;
						}
					}
				}
			}
		if(best < L)
			return best;
		// Rectangle
		if(L%4==0)
			return 0;
		return 1;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const double& Expected, const double& Received) {
 bool ok = (abs(Expected - Received) < 1e-9);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, FindPolygons().minimumPolygon(L));}
int main(){
CASE(0)
	int L = 4; 
	double _ = 0.0; 
END
CASE(1)
	int L = 5; 
	double _ = -1.0; 
END
CASE(2)
	int L = 12; 
	double _ = 2.0; 
END
CASE(3)
	int L = 4984; 
	double _ = 819.0; 
END
CASE(4)
	int L = 5000; 
	double _ = -999; 
END
CASE(4)
	int L = 20; 
	double _ = -999; 
END
/*
CASE(5)
	int L = ; 
	double _ = ; 
END
*/
}
// END CUT HERE
