module Main; // import BinaryHeap; // import BoolArray; // import CPLib.Arithmetic; // import CPLib.Bipartite; // import CPLib.Bits; // import CPLib.Convolution; // import CPLib.DSegtree; import CPLib.Graph; import CPLib.IO; // import CPLib.LDSegtree; // import CPLib.LSegtree; // import CPLib.MaxFlow; // import CPLib.Memoize; // import CPLib.MinCostFlow; // import CPLib.Misc; // import CPLib.Monoid; // import CPLib.Segtree; // import CPLib.String; import CPLib.Trait; // import CPLib.TwoSat; // import CPLib.UnionFind; // import CPLib.ZP; // import HashMap; // import HashSet; main : IO () = ( let iN = *read_i; let iM = *read_i; let iA = *read_i - 1; let iB = *read_i; let graph = GraphBuilder::create(iN + 3); let start = iN + 1; let end = iN + 2; let graph = *range(0, iM).fold_m(graph, |m, graph| let iL = *read_i - 1; let iR = *read_i; let graph = graph.add_edge(iL, iR, 1).add_edge(iR, iL, 1); let graph = if iL <= iA { graph.add_edge(start, iL, 0) } else { graph }; let graph = if iR <= iA { graph.add_edge(start, iR, 0) } else { graph }; let graph = if iB <= iR { graph.add_edge(iR, end, 0) } else { graph }; let graph = if iB <= iL { graph.add_edge(iL, end, 0) } else { graph }; graph.pure ); let ans = graph.build.dijkstra(start).@(end); let ans = if ans == inf { -1 } else { ans }; ans.to_string.println;; pure() );