#include<bits/stdc++.h>
using namespace std;
// use LLONG_MAX(the one you want) instead of LONG_MAX(INT_MAX)
#define endl "\n"
#define int long long 
int mod=1e9+7;
//const int INF=1e18;
signed main(){
#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
#endif
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);        
        int t=1;
        cin>>t;
        vector<int> num;
        int x=2050;
        for(int i=0;i<16;i++){
          num.push_back(x);
          x=x*10;
        }
        while(t--){
            int n;
            cin>>n;         
            int ans=0;
            int sz=num.size();
            sz--;
            while(sz>=0){
                // cout<<num[sz]<<endl;
               ans=ans+n/(num[sz]);
               n=n-(n/num[sz])*num[sz];
               sz--;
            }
            if(n!=0){
              cout<<-1<<endl;
            }
            else{
              cout<<ans<<endl;
            }
         }
        return 0;
}