A pyramid sequence of height X (with X>1) has period 2X−2. The first 2X−2 terms of one period are 1,2,…,X−1,X,X−1,…,2, and the sequence repeats that block forever. Terms are numbered from 1, as A[1],A[2],….
You are given the heights N and M of two pyramid sequences A and B. Count how many distinct ordered pairs (A[i],B[i]) occur over all i≥1.