#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define task "pieaters"
#define pll pair<ll, ll>
#define pii pair<pll, ll>
#define fi first
#define se second

using namespace std;
const ll mod = 998244353;
const ll N = 5005;
const int base = 313;
ll n, k, m, tong, ans, t, a[N], b[N], c[N], st[N], dp[N][N], d[N];
struct cow
{
    ll l, r, w;
    bool operator < (const cow& x)
    {
        return (l < x.l) ||(l == x.l && r < x.r);
    }
}p[N];
ll pw(ll k, ll n)
{
    ll total = 1;
    for(; n; n >>= 1)
    {
        if(n & 1)total = total * k % mod;
        k = k * k % mod;
    }
    return total;
}
vector<ll> kq;

ll C(ll u, ll v)
{
    if(u > v)return 0;
    return a[v] * b[u] % mod * b[v-u] % mod;
}
void update(ll id, ll l, ll r, ll pos, ll val)
{
    if(l == r)
    {
        st[id] = max(st[id], val);
        return;
    }
    ll mid = (l + r) / 2;
    if(mid >= pos)update(id*2, l, mid, pos, val);
    else update(id*2+1, mid+1, r, pos, val);
    st[id] = max(st[id*2], st[id*2+1]);
}
ll get(ll id, ll l, ll r, ll u, ll v)
{
    if(u <= l && r <= v)return st[id];
    if(u > v || u > r || l > v)return 0;
    ll mid = (l + r) / 2;
    return max(get(id*2, l, mid, u, v), get(id*2+1, mid+1, r, u, v));
}
ll hsh(ll i, ll j)
{
    return (a[j] - a[i-1] * b[j-i+1] % mod + mod) % mod;
}
void sol()
{
    cin >> n;
    if(n % 2050 != 0)cout << - 1 << '\n';
    else
    {
        ans = 0;
        n /= 2050;
        while(n > 0)
        {
            ans += n % 10;
            n /= 10;
        }
        cout << ans << '\n';
    }
}
/*
6
(()
2
(())()
(()())
*/
int main()
{
    if(fopen(task".in", "r"))
    {
       freopen(task".in", "r", stdin);
       freopen(task".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int ntest = 1;
    cin >> ntest;
    while(ntest -- > 0)
    sol();
}
/*
6 3
2 3 10 7 5 14
1 6
2 4
3 5

https://codeforces.com/contest/791/problem/E

*/
