#include<bits/stdc++.h>

using namespace std;

int main(){
    int t;
    cin>>t;
    while(t--){
        long long n,q;
        cin>>n;
        if(n<2050 || n%2050!=0){
            cout<<"-1"<<endl;
        }
        else{
          q=n/2050;
          int sum = 0;
          while(q){
              int r=q%10;
              sum +=r;
              q=q/10;
          }
          cout<<sum<<endl;
        }
    }
}