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

int main(){
	ll t;
	cin>>t;
	while(t--){
		ll n;
		cin>>n;
		if(n%2050!=0){
			cout<<-1<<"\n";
		}
		else{
			ll k = n/2050;
			ll ans = 0;
			while(k){
				ans += k%10;
				k/=10;
			}
			cout<<ans<<"\n";
		}
	}
}