#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fri(a,b) for(ll i=a;i<b;i++)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll sz(ll n){
  return (1+(ll)floor(log10(n)));
}
ll func(ll n){
  //cout<<n<<endl;
  if(!n) return 0;

 if(n<2050) return -1;
 /*
 ll temp=sz(n);
 ll f=n/pow(10,temp-1);
 if(f==1){
  ll ans=func(n-5*2050*pow(10,temp-5));
  if(ans==-1) return -1;
  else {return 5+ans;}
 }
 else if(f==2){
 ll ans=func(n-2050*pow(10,temp-4));
  if(ans==-1) return -1;
  else {return 1+ans;}
 }
 else{
 ll ans=func(n-(f/2)*2050*pow(10,temp-4));
  if(ans==-1) return -1;
  else {return f/2+ans;}

 }
*/
ll temp=2050;
while(temp*10<=n) temp*=10;
ll ans=func(n-temp);
if(ans==-1) return -1;
else{
  return ans+1;
}
}
void simply_solve(){
ll n;cin>>n;
cout<<func(n)<<endl;




}
int main()
{
    fast;
   int t=1;
   cin>>t;
   for(ll i=0;i<t;i++){
   // cout<<"Testcase number "<<i+1<<" :"<<endl;
    simply_solve();
   }


    return 0;
}
