#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <unistd.h>
#define rep(i,a,n) for(int i = a;i < n;i++)
#define fi first
#define se second
typedef long long ll;
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e4 + 3;
const int mod = 1e9 + 7; 
ll quick_pow(ll a, ll b){ll ans = 1;while(b){if(b & 1){ans = (ans * a) % mod;}a = (a * a) % mod;b >>= 1;}return ans;}

ll gcd(ll a, ll b){return (b == 0 ? a : gcd(b, a % b));}

struct cmp
{
	bool operator() (pair<int,pair<int,int>> a1,pair<int,pair<int,int>> a2){
		return a1.first > a2.first;
	}
};



int main(){
    ios::sync_with_stdio(false);
    //freopen("in.txt","r", stdin);
	int t;
	cin  >> t;
	while(t--){
		ll n;
		cin >> n;
		if(n / 2050 * 2050 == n){
			ll x = n / 2050;
			int cnt = 0;
			while(x){
				cnt += x % 10;
				x /= 10;
			}
			cout << cnt << endl;
		}else{
			cout << -1 << endl;
		}
	}

	
}
