#include<iostream>
using namespace std;


int main(){
    int t;
    cin>>t;
    while(t-- > 0){
        long long x;
        int sum=0;
        cin>>x;
        if(x%2050 != 0){
            cout<<-1<<"\n";
        }else{
            long long y = x/2050;
            while(y>0){
                sum+=y%10;
                y/=10;
            }
            cout<<sum<<"\n";
        }
        

    }
    return 0;
}