#include <iostream>
using namespace std;
#define ll unsigned long long
void solve() {
    ll n;
    cin>>n;
    if(n%2050) {
        cout<<-1<<endl;
    }
    else {
        n = n/2050;
        int ans = 0;
        while(n) {
            ans += n%10;
            n = n/10;
        }
        cout<<ans<<endl;
    }
}


int main() {
    int _t;
    cin>>_t;
    while(_t--) solve();
}