#include <bits/stdc++.h>
#define int long long
using namespace std;
int solve(int x)
{
	int k=x/2050,t=x%2050;
	if (t) return -1;
	int ans=0;
	while (k)
	{
		ans+=k%10;
		k/=10;
	}
	return ans;
}
signed main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int t;
	cin>>t;
	while (t--)
	{
		int n;
		cin>>n;
		cout<<solve(n)<<'\n';
	}
}