n, k, m = map(int, input().split())
cols = [[None, None] for i in range(n)]
for i in range(m):
    c, r, v = map(int, input().split())
    cols[c][r] = v
s = 0
for a in range(min(k+1, 19)):
    for b in range(min(k+1-a, 19)):
        c = k - a - b
        if c > 18:
            continue
        t = (a, b, c)
        p = 1
        for i, (x, y) in enumerate(cols):
            cs = t[i % 3]
            if x is None and y is None:
                p *= min(1 + cs, 19 - cs)
            elif x is not None and y is not None:
                p = 0
                break
            else:
                z = x if y is None else y
                if not (z <= cs <= z + 9):
                    p = 0
                    break
        s += p
print(s)
