CF601A.The Two Routes

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

In Absurdistan, there are nn towns (numbered 11 through nn ) and mm bidirectional railways. There is also an absurdly simple road network — for each pair of different towns xx and yy , there is a bidirectional road between towns xx and yy if and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.

A train and a bus leave town 11 at the same time. They both have the same destination, town nn , and don't make any stops on the way (but they can wait in town nn ). The train can move only along railways and the bus can move only along roads.

You've been asked to plan out routes for the vehicles; each route can use any road/railway multiple times. One of the most important aspects to consider is safety — in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town nn ) simultaneously.

Under these constraints, what is the minimum number of hours needed for both vehicles to reach town nn (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town nn at the same moment of time, but are allowed to do so.

输入格式

The first line of the input contains two integers nn and mm ( 2<=n<=4002<=n<=400 , 0<=m<=n(n1)/20<=m<=n(n-1)/2 ) — the number of towns and the number of railways respectively.

Each of the next mm lines contains two integers uu and vv , denoting a railway between towns uu and vv ( 1<=u,v<=n1<=u,v<=n , uvu≠v ).

You may assume that there is at most one railway connecting any two towns.

输出格式

Output one integer — the smallest possible time of the later vehicle's arrival in town nn . If it's impossible for at least one of the vehicles to reach town nn , output 1-1 .

输入输出样例

  • 输入#1

    4 2
    1 3
    3 4
    

    输出#1

    2
    
  • 输入#2

    4 6
    1 2
    1 3
    1 4
    2 3
    2 4
    3 4
    

    输出#2

    -1
    
  • 输入#3

    5 5
    4 2
    3 5
    4 5
    5 1
    1 2
    

    输出#3

    3
    

说明/提示

In the first sample, the train can take the route and the bus can take the route . Note that they can arrive at town 44 at the same time.

In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 44 .

首页