program main
   integer :: x(3),r(3),t(3) = (/ 4, -9, 5 /)
   read(*, *)n,m
   id = n - m/1000
   dmin = id/4.d0
   dmax = id*2.d0/9.d0
   k = floor(dmax)
   r = (/-id, 2*id, m/1000/)
   x = r + k*t
   if(any(x < 0)) x = -1
   write(*, *) x
end