Cinema
Time limit2sMemory limit1024 MB
Children fill an n by m hall row by row, then column by column. Count how many children end up in the same seat under both orders.
- Level
Medium5 of 10
- Topics
- Math, Number theory, Implementation, Matrix
- Solved
- No attempts yet
Problem
Maria Ivanovna and Maria Mikhailovna took the schoolchildren to a cinema. To avoid any hurt feelings, Maria Ivanovna lined up all the schoolchildren in alphabetical order and seated them: first the first row from left to right, then the second row from left to right, and so on, filling the entire hall of rows with seats each. Then Maria Mikhailovna came and said the children were seated wrong, so they had to move. She suggested filling all the first seats from the first row to the last, then all the second seats, and so on.
Determine how many schoolchildren stay in their seats after this reseating.
For example, if and , then in the first arrangement the children sit like this:
1 2 3
4 5 6
7 8 9
and in the second like this:
1 4 7
2 5 8
3 6 9
So three schoolchildren, 1, 5, and 9, stay in their seats.
Input
The input file contains two integers and ().
Output
Print the number of schoolchildren who stay in their seats.