import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

immutable int base = 2050;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(long);
		if (n % base != 0)
		{
			writeln (-1);
		}
		else
		{
			n /= base;
			writeln (n.text.map !(q{a - '0'}).sum);
		}
	}
}
