#include <bits/stdc++.h>
#define int long long

using namespace std;

int T, n;

signed main() {
    scanf("%lld", &T);
    while (T--) {
        scanf("%lld", &n);
        if (n % 2050) {
            puts("-1");
        } else {
            n = n / 2050;
            int ans = 0;
            while (n) {
                ans += n % 10;
                n /= 10; 
            }
            printf("%lld\n", ans);
        }
    }
    return 0;
}