Farmer John's cows keep escaping from his farm and causing trouble. To keep them from opening the pasture gate, he buys an expensive combination lock.
His cows are clever, so Farmer John wants to be sure they cannot open the lock just by trying many combinations. The lock has three dials, each numbered 1 through N (1≤N≤100). The dials are circular, so 1 and N are adjacent. Two combinations open the lock: the one Farmer John set, and a master combination set by the lock maker. The lock tolerates a little error, so it also opens when all three dials are within 2 positions of the corresponding numbers of one single combination. For example, if Farmer John's combination is (1,2,3) and the master combination is (4,5,6), the lock opens for (1,N,5) because that is close enough to Farmer John's combination, and for (2,4,8) because that is close enough to the master combination. It does not open for (1,5,6), since that is not close enough to either single combination.
Given Farmer John's combination and the master combination, determine how many distinct dial settings open the lock. Order matters, so (1,2,3) and (3,2,1) are different settings.
Every number in both combinations is between 1 and N.