This page is still under construction.

Parts of this page are still being built. What you see may change.

Sleepwalker

Time limit1sMemory limit128 MB

Summary
A self-similar walk on a 3^k by 3^k grid is defined by a recursive rewrite; given a starting tile on the walk and a hole tile, find the number of steps until the walk reaches the hole.
Level

Hard9 of 10

Topics
Recursion, Divide and conquer, Simulation, Math
Solved
No attempts yet

Problem

A building has a flat, square roof of size 3k×3k3^k \times 3^k, with its sides parallel to the north-south and east-west directions. The roof is covered with unit square tiles (each of side length 11), but one tile has been removed, leaving a hole large enough to fall through.

The tiles form a rectangular grid, so each tile has integer coordinates. The tile in the southwestern corner has coordinates (1,1)(1, 1). The first coordinate increases toward the east, and the second increases toward the north.

A sleepwalker wanders across the roof. In each step he moves from the tile he stands on to an adjacent tile: east (E), west (W), south (S), or north (N). His walk always starts from the southwestern corner tile. The walk is described by a word dkd_k over the letters N, S, E, W, where each letter denotes one step in that direction.

For k=1k = 1 the walk is:

d_1 = EENNWSWN

For k=2k = 2 the walk is:

d_2 = NNEESWSEENNEESWSEEEENNWSWNNEENNWSWNNEENNWSWNWWWSSENESSSSWWNENWWSSWWNENWNEENNWSWN

In general, for k≥1k \ge 1, the walk on a roof of size 3k+1×3k+13^{k+1} \times 3^{k+1} is built from the walk dkd_k as follows:

d_{k+1} = a(d_k) E a(d_k) E d_k N d_k N d_k W c(d_k) S b(d_k) W b(d_k) N d_k

Here aa, bb, and cc are permutations of the four direction letters:

functionEWNS
aaNSEW
bbSNWE
ccWESN

Applying a function to a word rewrites every letter according to its column in the table above. For example, a(SEN)=WNEa(SEN) = WNE, b(SEN)=ESWb(SEN) = ESW, and c(SEN)=NWSc(SEN) = NWS.

We begin watching the sleepwalker at the moment he stands on the tile (u1,u2)(u_1, u_2). After how many steps will he fall into the hole left by the removed tile at (v1,v2)(v_1, v_2)?

The pictures below show the sleepwalker's path on roofs of size 3×33 \times 3 and 9×99 \times 9. In the 9×99 \times 9 case, the tile where the observation begins and the hole are both marked.

Write a program that reads the roof size kk, the tile where the sleepwalker stands when the observation begins, and the tile that was removed to make the hole, then computes and prints how many steps the sleepwalker takes before falling into the hole.

Input

The first line contains one integer kk with 1≤k≤601 \le k \le 60, giving the roof size 3k×3k3^k \times 3^k. Each of the next two lines contains two integers xx and yy separated by a space, with 1≤x≤3k1 \le x \le 3^k and 1≤y≤3k1 \le y \le 3^k. The two numbers on the second line are the coordinates of the tile on which the sleepwalker stands when the observation begins. The two numbers on the third line are the coordinates of the hole. The input is guaranteed to be such that the sleepwalker eventually falls into the hole.

Output

Print a single line containing the number of steps on the sleepwalker's path from the starting tile to the hole.

Examples3

  1. Example 1

    Input
    2
    3 2
    7 2
    
    Expected output
    20
    
  2. Example 2

    Input
    1
    1 1
    1 3
    
    Expected output
    8
    
  3. Example 3

    Input
    1
    1 1
    2 1
    
    Expected output
    1