#include<bits/stdc++.h>
#define fast ios::sync_with_stdio(0), cin.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 3e2 + 10; 
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

void solve() {
    ll n;
    cin >> n;
    ll st = 2050;
    while(st < n) st *= 10;
    int ans = 0;
    while(n >= 2050) {
        while(st > n) st /= 10;
        if(n >= st) {
            n -= st;
            ++ ans;
        }
    }
    if(n) ans = -1;
    cout << ans << endl;
}

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

    return 0;
}