499A.md (2514B)
1 Source: https://codeforces.com/problemset/problem/499/A 2 3 A. Watching a movie 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 stdin 16 17 output 18 19 stdout 20 21 You have decided to watch the best moments of some movie. There are two buttons on your player: 22 23 1. Watch the current minute of the movie. By pressing this button, you watch the current minute of the movie and the player automatically proceeds to the next minute of the movie. 24 2. Skip exactly *x* minutes of the movie (*x* is some fixed positive integer). If the player is now at the *t*-th minute of the movie, then as a result of pressing this button, it proceeds to the minute (*t* + *x*). 25 26 Initially the movie is turned on in the player on the first minute, and you want to watch exactly *n* best moments of the movie, the *i*-th best moment starts at the *l**i*-th minute and ends at the *r**i*-th minute (more formally, the *i*-th best moment consists of minutes: *l**i*, *l**i* + 1, ..., *r**i*). 27 28 Determine, what is the minimum number of minutes of the movie you have to watch if you want to watch all the best moments? 29 30 Input 31 32 The first line contains two space-separated integers *n*, *x* (1 ≤ *n* ≤ 50, 1 ≤ *x* ≤ 105) — the number of the best moments of the movie and the value of *x* for the second button. 33 34 The following *n* lines contain the descriptions of the best moments of the movie, the *i*-th line of the description contains two integers separated by a space *l**i*, *r**i* (1 ≤ *l**i* ≤ *r**i* ≤ 105). 35 36 It is guaranteed that for all integers *i* from 2 to *n* the following condition holds: *r**i* - 1 < *l**i*. 37 38 Output 39 40 Output a single number — the answer to the problem. 41 42 Examples 43 44 Input 45 46 ``` 47 2 3 48 5 6 49 10 12 50 ``` 51 52 Output 53 54 ``` 55 6 56 ``` 57 58 Input 59 60 ``` 61 1 1 62 1 100000 63 ``` 64 65 Output 66 67 ``` 68 100000 69 ``` 70 71 Note 72 73 In the first sample, the player was initially standing on the first minute. As the minutes from the 1-st to the 4-th one don't contain interesting moments, we press the second button. Now we can not press the second button and skip 3 more minutes, because some of them contain interesting moments. Therefore, we watch the movie from the 4-th to the 6-th minute, after that the current time is 7. Similarly, we again skip 3 minutes and then watch from the 10-th to the 12-th minute of the movie. In total, we watch 6 minutes of the movie. 74 75 In the second sample, the movie is very interesting, so you'll have to watch all 100000 minutes of the movie.