This page is still under construction.

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

Train Trip

Time limit4sMemory limit1024 MB

Summary
Given N cities, each with a train covering an interval [L_i, R_i] that contains city i, find the fewest train rides from U to V, or -1 if impossible.
Level

Medium6 of 10

Topics
Shortest path, Array, Greedy
Solved
No attempts yet

Problem

Jaemin is worn out by the complicated, exhausting life at Gyeonggi Science High School, so he decides to leave for somewhere else. He sets off on a very long trip to Songjuk Kingdom to look for a quiet resort.

Songjuk Kingdom is a peaceful country with NN cities in a row. The cities are numbered from 1 at the far end up to NN.

Jaemin plans to travel on the Songjuk Train, a popular tourist attraction in the kingdom. Each city has one kind of train, and each train runs along its own fixed route. Specifically, the train departing from city ii runs on a circular line that passes through every city from city LiL_i to city RiR_i, where (1≤Li≤i≤Ri≤N)(1 \le L_i \le i \le R_i \le N). A passenger can get off at any city while the train is passing through, but cannot board a train partway through its route.

During the trip, Jaemin makes QQ travel plans. The ii-th plan is to travel from city UiU_i to city ViV_i using only the Songjuk Train.

Jaemin wants to save both time and money, so he wants to change trains as few times as possible in each plan. Your task is to determine whether each travel plan can be carried out using only trains, and if so, find the minimum number of trains he must ride.

Input

The first line contains NN and QQ, separated by a space. (1≤N≤200,000,1≤Q≤100,000)(1 \le N \le 200{,}000, 1 \le Q \le 100{,}000)

The next NN lines each contain LiL_i and RiR_i, separated by a space. (1≤Li≤i≤Ri≤N)(1 \le L_i \le i \le R_i \le N)

The next QQ lines each contain UiU_i and ViV_i, separated by a space. (1≤Ui,Vi≤N)(1 \le U_i, V_i \le N)

Output

For each travel plan, print the minimum number of trains that must be ridden to carry it out, one per line, for a total of QQ lines. If a plan cannot be carried out using only trains, print -1 instead of the number of trains.

Examples1

  1. Example 1

    Input
    6 4
    1 3
    1 2
    3 5
    4 6
    5 5
    5 6
    2 6
    5 1
    1 5
    4 4
    
    Expected output
    4
    -1
    2
    0