import numpy as np
N, M = 13, 17
A = [int(i) for i in '29 7 5 7 9 51 7 13 8 55 42 9 81'.split()]
ar = np.eye(N) * A

for h in range(0, N):
    for w in range(1, h+1):
        ar[-h-1, -h-1+w] = ar[-h, -h-1+w] + ar[-h-1, -h-2+w] - ar[-h, -h-2+w]
print(int(sum(sum(ar % M == 0)) - sum(np.linspace(1, N-1, N-1))))