Pyramid Sequences

Given heights N and M of two repeating pyramid sequences, count the distinct ordered pairs (A[i], B[i]) that appear.

Medium7MathNumber theoryImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

A pyramid sequence of height XX (with X>1X > 1) has period 2X22X-2. The first 2X22X-2 terms of one period are 1,2,,X1,X,X1,,21, 2, \ldots, X-1, X, X-1, \ldots, 2, and the sequence repeats that block forever. Terms are numbered from 1, as A[1],A[2],A[1], A[2], \ldots.

You are given the heights NN and MM of two pyramid sequences AA and BB. Count how many distinct ordered pairs (A[i],B[i])(A[i], B[i]) occur over all i1i \ge 1.

Input

The first line contains NN and MM, separated by a space. (2N,M1092 \le N, M \le 10^9)

Output

Print the number of distinct ordered pairs (A[i],B[i])(A[i], B[i]) on the first line.

Hint

For N=3N = 3 and M=4M = 4 the two sequences are:

  • AA: 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, ...
  • BB: 1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1, ...

The distinct pairs are (1,1)(1, 1), (2,2)(2, 2), (3,3)(3, 3), (2,4)(2, 4), (1,3)(1, 3), (3,1)(3, 1), so there are 6 of them.