public int numKeys(int[] lengths, int r1, int c1, int r2, int c2, int total, boolean rev1,
      boolean rev2) {
    if (r1 == r2)
      return Math.abs(c2 - c1) + total;
    if (c1 <= c2 || (rev1 && rev2)) {
      if (r1 > r2)
        r1--;
      else
        r1++;

      c1 = Math.min(c1, lengths[r1 - 1] + 1);
      total++;
      return numKeys(lengths, r1, c1, r2, c2, total, rev1, rev2);
    } else if (r1 == 1 || r1 == lengths.length) {
      total += c1 - c2;
      c1 = c2;
      return numKeys(lengths, r1, c1, r2, c2, total, rev1, rev2);
    } else {
      c1 = Math.min(c1, lengths[r1 - 1] + 1);
      if (r1 > r2) {
        // return Math.min(numKeys(lengths, r1, c1, r2, c2, total, true, rev2));
      }
    }
    return 0;
  }