#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin >> n;
        if (n % 2050)puts("-1");
        else {
            ll x = n / 2050;
            int res = 0;
            while (x) {
                res += x % 10;
                x /= 10;
            }
            cout << res << endl;
        }
    }
    
    return 0;
}