// In the name of God

#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define F first
#define S second

const int inf = 2e9;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;

ll n;

ll power(int x){
    return pow(1ll*10, x) + 0.5;
}

void solve(){
    cin >> n;
    int ans = 0;
    for(int i = 14; i >= 0; i--){
        ll x = 1ll*2050*power(i);
        while(n >= x){
            // cout << x << endl;
            n -= x;
            ans++;
        }
    }
    if(n != 0){
        cout << -1 << endl;
    } else {
        cout << ans << endl;
    }
}

int main(){
    // ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
     cin >> t;
    for(int i = 1; i <= t; i++){
       //  cout << "Case #" << i << ": ";
        solve();
    }
    return 0;
}
