Combination Lock

No attempts yetTime limit1sMemory limit128 MB

Problem

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 11 through NN (1N1001 \le N \le 100). The dials are circular, so 11 and NN 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)(1,2,3) and the master combination is (4,5,6)(4,5,6), the lock opens for (1,N,5)(1,N,5) because that is close enough to Farmer John's combination, and for (2,4,8)(2,4,8) because that is close enough to the master combination. It does not open for (1,5,6)(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)(1,2,3) and (3,2,1)(3,2,1) are different settings.

Input

  • Line 1: the integer NN.
  • Line 2: three space separated integers, Farmer John's combination.
  • Line 3: three space separated integers, the master combination. It may be the same as Farmer John's combination.

Every number in both combinations is between 11 and NN.

Output

  • Line 1: the number of distinct dial settings that open the lock.