#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define Y cout<<"YES"<<endl
#define N cout<<"NO"<<endl



int main()
{
	#ifndef ONLINE_JUDGE   
    freopen("input.txt", "r", stdin);   
    freopen("output.txt", "w", stdout);   
    #endif 
	ios::sync_with_stdio(0);
	cin.tie(0);

	int t;
	cin>>t;
	// t=1;
	while(t--)
	{
		ll n,k,c=0;
		cin>>n;
		// ll a[n];
		// for(int i=0;i<n;i++) cin>>a[i];
		k=2050;
		while(n>=k)
		{
			while(k*10<=n)
			{
				k=k*10;
			}
			n=n-k;
			c++;
			k=2050;
		}
		if(n==0) cout<<c<<endl;
		else
		cout<<-1<<endl;

	}
}
