#include <iostream>
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
	
	int t;
	cin>>t;
	
	while(t--) {
	    ll n;
	    cin>>n;
	    
	    //a + 10b + 100c + 1000d + 10000e + ......
	    ll ans = 0;
	    bool flag = true;
	    if(n%2050!=0) {
	        flag = false;
	        //cout<<-1<<endl;
	    }else {
	        ll k;
	        while(n>0) {
	            if(n<2050) {
	                flag = false;
	                //cout<<-1<<endl;
	                break;
	            }
	            k = 2050;
	            
	            while(k <= n) k*=10;
	            k /= 10;
	            
	            n -= k;
	            ans++;
	        }
	    }
	    if(!flag) ans = -1;
	    cout<<ans<<endl;
	}
	
	return 0;
}
