Contests Virtual Contests Problems Submit Runs Status Rank List Forum

2828.   The Flower Garden
Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 579   Accepted Runs: 408    Multiple test files

Imagine Betsy's surprise as she rounded the barn and discovered that Farmer John had built a secret greenhouse that was now brimming with gorgeous flowers. Her mind ran wild as visions of a gorgeous colorful garden swirled through her little bovine brain.

"I think I'll make a long row of F (7 ≤ F ≤ 10,000) flowers against the far fence," she thought. "I'll plant roses in every 3rd slot, begonias in every 7th slot that is still open, and daisies in every 4th slot that is still open." Betsy wondered how many open slots would remain. She realized that the number would depend on which slot she started planting when she intended to fill every N-th slot with a kind of flower.

Help Betsy know how many open slots will remain. Read a set of K (1 ≤ K ≤ 100) planting descriptors, each of which tells a starting location L (1 ≤ LF) -- L = 1 is the first flower -- and an interval I (1 ≤ IF) for planting flowers. Deduce the number of empty slots that remain after planting the entire set.

If Betsy followed through on her initial vision, she might specify the planting as:

      30 3     [30 slots total; 3 kinds of flowers]
1 3      [start at slot 1 and plant roses every 3rd slot]
3 7      [start at slot 3 and plant begonias every 7rd slot]
1 4      [start at slot 1 and plant daisies in every 4th slot]

Thus, the empty garden looks like this:
     . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Then, after the rose planting:
     R . . R . . R . . R . . R . . R . . R . . R . . R . . R . .

Then, after the begonia planting:
     R . B R . . R . . R . . R . . R B . R . . R . B R . . R . .

Then, after the daisy planting:
     R . B R D . R . D R . . R . . R B . R . D R . B R . . R D .

13 empty slots remain after all the planting.

### Input

* Line 1: Two space-separated integers: F and K

* Lines 2..K + 1: Line j contains two space-separated integers that specify the planting of one kind of flower: Lj and Ij

### Output

* Line 1: A single line with a single integer that is the number of empty flower slots that remain after the planting is complete

### Sample Input

30 3
1 3
3 7
1 4

### Sample Output

13

Source: USACO Open Competition 2007
Submit   List    Runs   Forum   Statistics

Tianjin University Online Judge v1.3.0
Maintance:Fxz. Developer: SuperHacker, G.D.Retop, Fxz