#include <cstdio>
using namespace std;
typedef long long ll;
ll t,n,ans;
inline ll rd(){
	register ll x=0;
	register char c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x;
}
int main(){
	t=rd();
	while(t--){
		n=rd(),ans=0;
		if(n%2050){printf("-1\n");continue;}
		n/=2050;
		while(n){
			ans+=n%10;
			n/=10;
		}
		printf("%lld\n",ans);
	}
	return 0;
}