#include <bits/stdc++.h>
using namespace std;
 
int main(){
	long long int t;
	cin>>t;
	while(t--){
		long long int n;
		cin>>n;
		
		long long int k,l;
		k = n/2050;
		l = n%2050;
		if(l != 0){
			cout<<"-1"<<endl;
		}
		else{
			int sum = 0;
        while (k != 0) {
            sum = sum + k % 10;
            k = k / 10;
        }
        cout<<sum<<endl;
		}
	}
return 0;
}
