#include <bits/stdc++.h>
using namespace std;
 
typedef long double ld;
const long long inf = 1100000000000000000;
#define maxn 1000005
#define mod 1000000007 //998244353 
#define int long long 
#define PB push_back
#define F first
#define S second
#define vi vector<int>
#define rep(i,a,b)  for( int i = a; i < b; i++ )
#define rep1(i,a,b) for( int i = a; i <= b; i++)
#define repd(i,a,b) for( int i = a; i >= b; --i)
#define sz(x) (int)((x).size())  
#define all(c) (c).begin(),(c).end()
#define allr(v) v.rbegin(),v.rend()
#define mem(a,x) memset(a,x,sizeof(a))
#define dbg(a) cout << #a << " is " << (a); cout << "\n";

//...
void solve(){
	int n; cin>>n;
	int x = 2050;
	int ans = 0;
	while(n >= x){
		int c = x;
		while(c*10 <= n)
			c *= 10;
		n -= c; ans++;
	}
	if(n != 0)
		ans = -1;
	cout<<ans;
}

signed main(){
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
	int T=1; cin>>T; 
    while(T--){
        solve(); cout<<'\n';
    }
    return 0;
}