Kingdom of Magic

Interview

Time limit2sMemory limit128 MB

Summary
On a graph of up to 100 cities, two people who must always sit on adjacent distinct vertices move to a target adjacent pair, minimizing total single or paired moves.
Level

Medium7 of 10

Topics
Graph, BFS, Shortest path, Implementation
Solved
No attempts yet

Problem

The Kingdom of Magic has a network of bidirectional magic portals connecting its cities. Each portal directly connects a pair of cities and lets people travel quickly between them. Two cities joined by a portal are called neighboring.

Prince Albert and Princess Betty live in two neighboring cities and are deeply in love, yet they have never met and refuse to ever be in the same city at the same time. Therefore, wherever they go, they must always stay in a pair of neighboring, distinct cities (two different cities directly connected by a portal).

To move, they use the portals. In a single step they may either:

  • move just one of them through one portal (the other stays put), or
  • move both of them at the same time, each through a different portal.

They may never move through the same portal at the same time. After every step they must again occupy a pair of neighboring, distinct cities.

The cost of a step is the number of people who move: moving one person costs 11 move, and moving both at once costs 22 moves.

Given the portal network together with Albert's and Betty's starting cities and their destination cities, compute the minimal total number of moves needed to bring them from their starting pair of neighboring cities to their target pair of neighboring cities. It is guaranteed that this is possible.

Input

The first line contains six integers nn, mm, a1a_1, b1b_1, a2a_2, b2b_2. Here nn (3≤n≤1003 \le n \le 100) is the number of cities (numbered from 11 to nn) and mm (2≤m≤10002 \le m \le 1000) is the number of portals. Albert and Betty start in the neighboring cities a1a_1 and b1b_1 (1≤a1,b1≤n1 \le a_1, b_1 \le n, a1≠b1a_1 \ne b_1) and want to reach the neighboring cities a2a_2 and b2b_2 (1≤a2,b2≤n1 \le a_2, b_2 \le n, a2≠b2a_2 \ne b_2), with a1≠a2a_1 \ne a_2 or b1≠b2b_1 \ne b_2.

Each of the next mm lines contains two integers pi1p_{i1} and pi2p_{i2} (1≤pi1,pi2≤n1 \le p_{i1}, p_{i2} \le n, pi1≠pi2p_{i1} \ne p_{i2}), the two cities connected by that portal. At most one portal connects any pair of cities.

Output

Print a single integer: the minimal total number of moves needed to bring Albert and Betty from (a1,b1)(a_1, b_1) to (a2,b2)(a_2, b_2).

Examples2

  1. Example 1

    Input
    4 5 1 2 2 1
    1 2
    2 3
    3 4
    4 1
    1 3
    
    Expected output
    3
    
  2. Example 2

    Input
    3 3 1 2 1 3
    1 2
    2 3
    1 3
    
    Expected output
    1