#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while(t--){
        ll n;
        cin >> n;
        if(n%2050 != 0)
            cout << -1 << '\n';
        else
        {
            ll num = n/2050;
            int x = 0;
            while(num)
                x+=num%10, num = num/10;
            cout << x << '\n';
        }
    }
}