Problem:
Wire

I am a craftsman specialized in interior works. A customer asked me to perform wiring work on a wall whose entire rectangular surface is tightly pasted with pieces of panels. The panels are all of the same size (2 m in width, 1 m in height) and the wall is filled with an x (horizontal) by y (vertical) array of the panels. The customer asked me to stretch a wire from the left top corner of the wall to the right bottom corner whereby the wire is tied up at the crossing points with the panel boundaries (edges and vertexes) as shown in the figure. There are nine tied-up points in the illustrative figure shown below.

Fig: The wire is tied up at the edges and vertexes of the panels (X: 4 panels, Y: 6 panels)

Write a program to provide the number of points where the wire intersects with the panel boundaries.
Assume that the wire and boundary lines have no thickness.

Input

The input is given in the following format.

x y

A line of data is given that contains the integer number of panels in the horizontal direction x (1 ≤ x ≤ 1000) and those in the vertical direction y (1 ≤ y ≤ 1000).

Output

Output the number of points where the wire intersects with the panel boundaries.

Sample Input 1

4 4

Sample Output 1

5

Sample Input 2

4 6

Sample Output 2

9

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Text;

namespace AtTest.AOJ
{
    class ElectricWire
    {
        static void Main(string[] args)
        {
            Method(args);
            Console.ReadLine();
        }

        static void Method(string[] args)
        {
            int[] xy = ReadInts();
            int x = xy[0];
            int y = xy[1];
            int a = Math.Max(x, y);
            int b = Math.Min(x, y);
            while (b > 0)
            {
                int tmp = b;
                b = a % b;
                a = tmp;
            }
            int gcd = a;
            int unitX = x / gcd;
            int unitY = y / gcd;
            int unitCnt = unitX - 1 + unitY - 1 + 1;
            Console.WriteLine(unitCnt * gcd + 1);
        }

        private static string Read() { return Console.ReadLine(); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}