fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Main
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. long m, n, a, b;
  10. Scanner in = new Scanner(System.in);
  11. m = in.nextLong();
  12. n = in.nextLong();
  13. a = in.nextLong();
  14. b = in.nextLong();
  15. long dm = ((a + 1)/2 - 1) %m - ((b + 1)/2 - 1) % m;
  16. long dn = (a - 1)/(2 * m) - (b - 1)/(2 * m);
  17. long o = dm * dn < 0? Math.min(Math.abs(dm), Math.abs(dn)) : 0;
  18. long k = dm > 0? 1: -1;
  19. a += 2*k*o*(m - 1);
  20. System.out.print( ((a + b) % 2 == 0? 0: Math.max(a, b) % 2 == 0? 1: -1) + 2*(Math.abs(dm) + Math.abs(dn) - o));
  21.  
  22. }
  23. }
Success #stdin #stdout 0.07s 2184192KB
stdin
5 4
 1 38
stdout
13