This page is still under construction.

Parts of this page are still being built. What you see may change.

Cinema

Time limit2sMemory limit1024 MB

Summary
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 nn rows with mm 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 n=3n = 3 and m=3m = 3, 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 nn and mm (1≤n,m≤1091\le n, m\le 10^9).

Output

Print the number of schoolchildren who stay in their seats.

Examples2

  1. Example 1

    Input
    3 3
    
    Expected output
    3
    
  2. Example 2

    Input
    2 4
    
    Expected output
    2