#include<iostream>
using namespace std;

int main(){
    int t;
    long long n,sum,q,rem,j;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>n;
        sum=0;
        q=n/2050;
        rem=n%2050;
        while(true)
        {
            if(q==0)
            break;
            sum=sum+q%10;
            q=q/10;
        }
        if(rem==0)
        cout<<sum<<endl;
        if(rem!=0)
        cout<<"-1"<<endl;
    }
    return 0;
}