#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef vector<int> vi;
#define OJ \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout);

int main() 
{
    // OJ
    int t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        if(n%2050 != 0) cout<<-1<<endl;
        else{
            ll div = n/2050;
            int ans = 0;
            while(div){
                ans+= div%10;
                div /= 10;
            }
            cout<<ans<<endl;
        }
    }    
    return 0; 
}