PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000106. Khách sạn và thang máy (Hotels)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Khách sạn và thang máy (Hotels)

Hotels

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

A hotel has FF floors numbered 00 to F−1F-1; guests enter at floor 00. Elevator ii stops at floors ff satisfying f≡Ri(modMi)f\equiv R_i\pmod{M_i}. Adjacent floors can be traversed by stairs at cost one; travel between any stops of a given elevator costs zero. For each destination floor compute the fewest stairs from ground. Output the worst value and the lowest floor attaining it.

Input

First line F,EF,E. Each of the next EE lines gives elevator parameters Ri,MiR_i,M_i, with stops on floors f<Ff<F congruent to RiR_i modulo MiM_i.

Output

Print two integers S,WS,W: the greatest minimum stair count, and the lowest floor attaining it.

Subtasks

  • Subtask 1 (40 points): 2≤F≤30,  0≤E≤32\le F\le 30,\;0\le E\le 3. All input-format and graph conditions above apply.

  • Subtask 2 (60 points): 2≤F≤150,  0≤E≤72\le F\le 150,\;0\le E\le 7. All input-format and graph conditions above apply.

Examples

Input

5 1
0 2

Output

1 1

Explanation

The elevator stops on floors 0, 2, 4. These floors require no stairs; floors 1 and 3 each require one. The maximum is 1 and the lowest achieving floor is 1.