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

bool cmp(pair<int,int> &a,pair<int,int> &b)
{
    if(a.first==b.first)
        return a.second<b.second;
    return a.first<b.first;
}

void solve()
{
    long long int n;
    cin>>n;
    if(n%2050!=0)
        cout<<-1<<endl;
    else
    {
        long long int t=n/2050;
        long long int cnt=0;
        while(t>0)
        {
            cnt+=t%10;
            t=t/10;
        }
        cout<<cnt<<endl;
    }
}

int main()
{
    int t;
    cin>>t;
    while(t--)
        solve();
}