#include<bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;

int main()
{
	fio
// 	#ifndef ONLINE_JUDGE
// 	    freopen("input.txt", "r", stdin);
// 	    freopen("output.txt", "w", stdout);
// 	#endif
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin >> n;
		if(n%2050)
		{
			cout << -1 << '\n';
			continue;
		}
		ll ans = INT_MAX;
		ll x = 2050;
		while(x*10LL<=n)
		{
			ans = min(ans, n/x);
			x*=10LL;
		}
// 		cout << x << '\n';
		ll c = 0;
		while(n>0)
		{
			c = c + (n/x);
			ll b = n/x;
			n = n - (b*x);
			x/=10;
		}
		cout << c << '\n';

	}
	return 0;
}