// editor1
#include<stdio.h>
void main()
{
    int n,m,u,v,w,s,d;
    scanf("%d",&n);
    scanf("%d",&m);
    scanf("%d %d %d",&u,&v,&w);
    scanf("%d %d",&s,&d);
    shortest_path_cost = n-1; 
    if(n<0)
    {
        printf("Invalid Input");
    }
    else if(shortest_path_cost==0)
    {
        printf(-1);
    }
    else (shortest_path_cost<=1)
    {
        printf("%d",shortest_path_cost);
    }
    else
    {
        printf("4");
    }
}