luogu#P7901. 「PMOI-0」一笔画

「PMOI-0」一笔画

Background

(Originally LZOI-1. The rename has been approved by PMOI members.)

Problem Description

Yan Zhuo dd is at a point with coordinates (x,y)(x,y) in a 2n×2n2n \times 2n matrix.

Now lhm needs to arbitrarily choose a point in the matrix as the starting point, then visit kk points in order, with the following movement rules:

  • Each move can only choose a point adjacent (up, down, left, or right) to the current point as the target point of this move. Points outside the matrix cannot be chosen.

  • In each round of movement, the path must visit all points in the matrix exactly once, and no point may be visited more than once. After visiting all points, that round of movement ends.

  • Note that after visiting kk points in order, the last round of movement may not be completed.

Now lhm wants to know: at minimum, how many times will he pass through Yan Zhuo dd.

Input Format

The first line contains four positive integers n,k,x,yn,k,x,y.

Output Format

Output one integer on the first line, representing the answer.

2 16 1 1
1

Hint

Sample Explanation

d\color{red}d represents Yan Zhuo dd's position, and the red line shows lhm's path in the first round.

Constraints

This problem uses bundled testdata.

Subtask ID Score n,kn,k\le
11 3030 10410^4
22 10910^9
33 4040 101810^{18}

For all testdata, it is guaranteed that 1n,k10181\le n,k\le10^{18} and 1x,y2n1\le x,y\le 2n.

Translated by ChatGPT 5