#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define mod 1000000007
#define int long long
#define ll long long
#define EPS 0.00000000000001
#define ld long double
#define inf 1e9
#define N 100005
#define K 33
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int gcd(int a,int b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}

int32_t main()
{
    IOS;
    #ifndef ONLINE_JUDGE
    freopen("int.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        int cnt = 0;
        for(int i = 14;i >= 0;i--) {
            int z = 2050;
            for(int j = 1;j <= i;j++)
                z *= 10;
            cnt += n / z;
            n = n % z;
        }
        cnt = (n == 0) ? cnt : -1;
        cout << cnt;
        cout << endl;
    }
    return 0;    
}    