#include <bits/stdc++.h>
#define int long long int

using namespace std;

const int N = 1e5 + 5;

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t;
    cin >> t;
    int f[15];
    f[0] = 2050;
    for(int i=1;i<15;i++)
        f[i] = f[i-1] * 10;
    while(t--)
    {
        int n;
        cin >> n;
        int ans = 0;
        for(int i = 14;i>=0;i--)
        {
            int val = (n / f[i]);
            ans += val;
            n -= val*f[i];
        }
        if(n)
            cout << -1 << '\n';
        else
            cout << ans << "\n";
    }
}
