Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

// 文字列を1行取得
func StrStdin() (stringReturned string) {
//	scanner := bufio.NewScanner(os.Stdin)

	scanner.Scan()
	stringInput := scanner.Text()

	stringReturned = strings.TrimSpace(stringInput)
	return
}

// 整数値1つ取得
func IntStdin() (int, error) {
	stringInput := StrStdin()
	return strconv.Atoi(strings.TrimSpace(stringInput))
}

// 空白や空文字だけの値を除去したSplit()
func SplitWithoutEmpty(stringTargeted string, delim string) (stringReturned []string) {
	stringSplited := strings.Split(stringTargeted, delim)

	for _, str := range stringSplited {
		if str != "" {
			stringReturned = append(stringReturned, str)
		}
	}

	return
}

// デリミタで分割して文字列スライスを取得
func SplitStrStdin(delim string) (stringReturned []string) {
	// 文字列で1行取得
	stringInput := StrStdin()

	// 空白で分割
	// stringSplited := strings.Split(stringInput, delim)
	stringReturned = SplitWithoutEmpty(stringInput, delim)

	return
}

// デリミタで分割して整数値スライスを取得
func SplitIntStdin(delim string) (intSlices []int, err error) {
	// 文字列スライスを取得
	stringSplited := SplitStrStdin(" ")

	// 整数スライスに保存
	for i := range stringSplited {
		var iparam int
		iparam, err = strconv.Atoi(stringSplited[i])
		if err != nil {
			return
		}
		intSlices = append(intSlices, iparam)
	}
	return
}

func main() {
	nk, _ := SplitIntStdin(" ")
	n, k := nk[0], nk[1]
	kind := make([]int, n)
	for i := 0; i < k; i++ {
		var tmp []int
		var err error
		if tmp, err = SplitIntStdin(" "); err != nil {
			panic(err)
		}
		kind[tmp[0]-1] = tmp[1]
	}
	dp := make([][3][3]int, n)
	if kind[0] > 0 {
		dp[0][0][kind[0]-1] = 1
	} else {
		for i := 0; i < 3; i++ {
			dp[0][0][i] = 1
		}
	}
	if kind[1] > 0 {
		for i := 0; i < 3; i++ {
			dp[1][i][kind[1]-1] = dp[0][0][i]
		}
	} else {
		for i := 0; i < 3; i++ {
			for j := 0; j < 3; j++ {
				dp[1][i][j] = dp[0][0][i]
			}
		}
	}
	for i := 1; i < n-1; i++ {
		if kind[i+1] > 0 {
			for j := 0; j < 3; j++ {
				if j == kind[i+1]-1 {
					for k := 0; k < 3; k++ {
						if j != k {
							dp[i+1][j][kind[i+1]-1] += dp[i][k][j]
							dp[i+1][j][kind[i+1]-1] %= 10000
						}
					}
				} else {
					for k := 0; k < 3; k++ {
						dp[i+1][j][kind[i+1]-1] += dp[i][k][j]
						dp[i+1][j][kind[i+1]-1] %= 10000
					}
				}
			}
		} else {
			for j := 0; j < 3; j++ {
				for k := 0; k < 3; k++ {
					for l := 0; l < 3; l++ {
						if j != k || k != l {
							dp[i+1][k][j] += dp[i][l][k]
							dp[i+1][k][j] %= 10000
						}
					}
				}
			}
		}
	}
	result := 0
	for i := 0; i < 3; i++ {
		for j := 0; j < 3; j++ {
			result += dp[n-1][i][j]
		}
	}
	// fmt.Println(dp)
	// fmt.Println(kind)
	fmt.Println(result % 10000)
}

Python: n, k = map(int,input().split())
dic = [0 for i in range(n + 1)]
for i in range(k):
  a, b = map(int,input().split())
  dic[a] = b

dp = [[0] * 3 for i in range(3)]
temp = [[0] * 3 for i in range(3)]
first = dic[1]
second = dic[2]

if not first and not second:
  for i in range(3):
    for j in range(3):
      dp[i][j] = 1

elif not first:
  for i in range(3):
    dp[i][second - 1] = 1

elif not second:
  for i in range(3):
    dp[first - 1][i] = 1

else:
  dp[first - 1][second - 1] = 1

for i in range(3, n + 1):
  past = dic[i]
  if not past:
    for y in range(3):
      for x in range(3):
        temp[x][y] = sum(dp[j][x] for j in range(3) if not (x == y and x == j))
  else:
    for y in range(3):
      for x in range(3):
        if y == past - 1:
          temp[x][y] = sum(dp[j][x] for j in range(3) if not (x == y and x == j))
        else:
          temp[x][y] = 0
  
  for x in range(3):
    for y in range(3):
      dp[x][y] = temp[x][y]
print(sum([dp[i][j] for i in range(3) for j in range(3)]) % 10000)

Python: import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop
import copy
from test.support import _MemoryWatchdog

BIG_NUM = 2000000000
HUGE_NUM = 99999999999999999
#MOD = 1000000007
MOD = 10000
EPS = 0.000000001
sys.setrecursionlimit(100000)


tomato = 0
cream = 1
basil = 2
tomato_2 = 3
cream_2 = 4
basil_2 = 5

N,K = map(int,input().split())
dp = [[0]*6 for _ in range(N)]
decided = [-1]*N

#決まってる日
for _ in range(K):
    A,B = map(int,input().split())
    decided[A-1] = B-1

#初日
if decided[0] == -1:
    for i in range(3):
        dp[0][i] = 1
else:
    dp[0][decided[0]] = 1

for i in range(1,N):

    if decided[i] == -1:   #メニューが決まってない
        for pre in range(6):
            for tommorow in range(6):
                if pre%3 != tommorow%3: #別の種類
                    if tommorow >= 3: #2日連続ではない
                        continue
                    dp[i][tommorow] += dp[i-1][pre]
                    dp[i][tommorow] %= MOD
                else: #同じ種類
                    if pre < 3 and tommorow >= 3:
                        dp[i][tommorow] += dp[i-1][pre]
                        dp[i][tommorow] %= MOD
    else: #メニューが決まっている
        for pre in range(6):
            if pre%3 != decided[i]%3: #別種
                dp[i][decided[i]] += dp[i-1][pre]
                dp[i][decided[i]] %= MOD
            else: #同種
                if pre >= 3:
                    continue
                dp[i][decided[i]+3] += dp[i-1][pre]
                dp[i][decided[i]+3] %= MOD


ans = 0
for i in range(6):
    ans += dp[N-1][i]
    ans %= MOD

print("%d"%(ans))

Python: #f = open("input.txt")
#N, K = [int(x) for x in f.readline().split(' ')]
#lines = f.readlines()
#f.close()
import sys
N, K = [int(x) for x in sys.stdin.readline().split(' ')]
lines = sys.stdin.readlines()


schedule = [0]*N
for line in lines:
    strs = line.split(' ')
    schedule[int(strs[0])-1] = int(strs[1])

number = [0]*9
for i in range(9):
    l1 = i // 3
    l2 = i % 3
    if(schedule[0]!=l2+1 and schedule[0]!=0):
        number[i] = 0
    elif(schedule[1]!=l1+1 and schedule[1]!=0):
        number[i] = 0
    else:
        number[i] = 1


for s in schedule[2:]:
    new_number = [0]*9
    for i in range(9):
        l1 = i // 3
        l2 = i % 3
        for j in range(3):
            new_number[i] += number[l2*3 + j] if((s==0 or s==l1+1) and  not (l2==j and l1==j)) else 0
    for i in range(9):
        number[i] = new_number[i] % 10000
sum = 0
for n in number:
    sum += n
print(sum % 10000)

Python: n,k = map(int,input().split())

table = [[0]*n for i in range(3)]

for _ in range(k):
	x,y=map(int,input().split())
	for i in range(3):
		table[i][x-1] = 0 if i == y-1 else -1

for i in range(3):
	if table[i][0] != -1:
		table[i][0] = 1

for i in range(1,n):
	s = 0
	for j in range(3):
		s += table[j][i-1] if table[j][i-1] != -1 else 0

	for j in range(3):
		if table[j][i] == -1:
			continue

		table[j][i] = s%10000

		if table[j][i-1] != -1 and table[j][i-2] != -1:
			if i == 1:
				continue
			table[j][i-1] -= table[j][i-2]
			table[j][i] -= table[j][i-2]

total = 0
for i in range(3):
	total += table[i][n-1] if table[i][n-1] != -1 else 0
total = total % 10000
print(total)

Python: #f = open("input.txt")
#N, K = [int(x) for x in f.readline().split(' ')]
#lines = f.readlines()

import sys
N, K = [int(x) for x in sys.stdin.readline().split(' ')]
lines = sys.stdin.readlines()
f.close()

schedule = [0]*N
for line in lines:
    strs = line.split(' ')
    schedule[int(strs[0])-1] = int(strs[1])

number = [0]*9
for i in range(9):
    l1 = i // 3
    l2 = i % 3
    if(schedule[0]!=l1+1 and schedule[0]!=0):
        number[i] = 0
    elif(schedule[1]!=l2+1 and schedule[1]!=0):
        number[i] = 0
    else:
        number[i] = 1


for s in schedule[2:]:
    new_number = [0]*9
    for i in range(9):
        l1 = i // 3
        l2 = i % 3
        for j in range(3):
            new_number[i] += number[l2*3 + j] if((s==0 or s==l1+1) and  not (l2==j and l1==j)) else 0
    for i in range(9):
        number[i] = new_number[i] % 10000
      
sum = 0
for n in number:
    sum += n
print(sum % 10000)