858B.md (2341B)
1 Source: https://codeforces.com/problemset/problem/858/B 2 3 B. Which floor? 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 In a building where Polycarp lives there are equal number of flats on each floor. Unfortunately, Polycarp don't remember how many flats are on each floor, but he remembers that the flats are numbered from 1 from lower to upper floors. That is, the first several flats are on the first floor, the next several flats are on the second and so on. Polycarp don't remember the total number of flats in the building, so you can consider the building to be infinitely high (i.e. there are infinitely many floors). Note that the floors are numbered from 1. 22 23 Polycarp remembers on which floors several flats are located. It is guaranteed that this information is not self-contradictory. It means that there exists a building with equal number of flats on each floor so that the flats from Polycarp's memory have the floors Polycarp remembers. 24 25 Given this information, is it possible to restore the exact floor for flat *n*? 26 27 Input 28 29 The first line contains two integers *n* and *m* (1 ≤ *n* ≤ 100, 0 ≤ *m* ≤ 100), where *n* is the number of the flat you need to restore floor for, and *m* is the number of flats in Polycarp's memory. 30 31 *m* lines follow, describing the Polycarp's memory: each of these lines contains a pair of integers *k**i*, *f**i* (1 ≤ *k**i* ≤ 100, 1 ≤ *f**i* ≤ 100), which means that the flat *k**i* is on the *f**i*-th floor. All values *k**i* are distinct. 32 33 It is guaranteed that the given information is not self-contradictory. 34 35 Output 36 37 Print the number of the floor in which the *n*-th flat is located, if it is possible to determine it in a unique way. Print -1 if it is not possible to uniquely restore this floor. 38 39 Examples 40 41 Input 42 43 ``` 44 10 3 45 6 2 46 2 1 47 7 3 48 ``` 49 50 Output 51 52 ``` 53 4 54 ``` 55 56 Input 57 58 ``` 59 8 4 60 3 1 61 6 2 62 5 2 63 2 1 64 ``` 65 66 Output 67 68 ``` 69 -1 70 ``` 71 72 Note 73 74 In the first example the 6-th flat is on the 2-nd floor, while the 7-th flat is on the 3-rd, so, the 6-th flat is the last on its floor and there are 3 flats on each floor. Thus, the 10-th flat is on the 4-th floor. 75 76 In the second example there can be 3 or 4 flats on each floor, so we can't restore the floor for the 8-th flat.