#include <bits/stdc++.h>
#include <stack>
using namespace std;
#define ll long long
#define rep(i,a,b) for(ll i=a; i<b; i++)
#define repi(i,a,b) for(ll i=b-1; i>=a; i--)
#define pb push_back
#define mod 1000000007
void fastscan(ll &num){
    bool n=false;
    register ll c;
    num=0;
    c=getchar();
    if(c=='-'){
        n=true;
        c=getchar();
    }
    for(;(c>47 && c<58); c=getchar()){
        num=num*10+c-48;
    }
    if(n){
        num*=-1;
    }
    
}
ll max(ll a, ll b) {if(a>b){return a;}else {return b;}}
ll min(ll a, ll b) {if(a>b){return b;}else {return a;}}

ll gcd(ll a, ll b){
    if(a>b){
        if(b==0){
            return a;
        }else {
            return gcd(a%b,b);
        }
    }else {
        if(a==0){
            return b;
        }else {
            return gcd(b%a,a);
        }
    }
}

bool isprime(ll n){
    if(n<=1){
        return false;
    }else if(n<=3){
        return true;
    }
    if(n%2==0 || n%3==0){
        return false;
    }
    for(int i=5; i*i<=n; i=i+6){
        if(n%i==0 || n%(i+2)==0){
            return false;
        }
    }
    return true;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll t,n;
    cin>>t;
    rep(i,0,t){
        cin>>n;
        ll x=2050,y=0;
        while(n>=x){
            x*=10;
        }
        x/=10;
        while(n>=2050){
            if(x<=n){
                n-=x;
                y++;
            }else {
                x/=10;
            }
        }
        if(n==0){
            cout<<y<<endl;
        }else {
            cout<<-1<<endl;
        }
    }
	// your code goes here
	return 0;
}
