#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
	ll t = 0, n = 0;
	cin >> t;
	while(t--)
	{
		cin >> n;
		if(n < 2050 || n % 2050 != 0) cout << -1;
		else
		{
			n/=2050;
			ll Cnt = 0;
			while(n > 0)
			{
				Cnt+=(n % 10);
				n/=10;
			}
			cout << Cnt;
		}
		cout << endl;
	}
}