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: #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]!=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)

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

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

number = [1]*9
for i in range(9):
    (l1,l2) = divmod(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

for s in schedule[2:]:
    tmp = [0]*9
    for i in range(9):
        (l1,l2) = divmod(i,3)
       	tmp[i] = [number[l2*3+j] for j in range(3) \
                 if (s==0 or s==l1+1) and  not (l2==j and l1==j)]
    number = [sum(tmp_i) for tmp_i in tmp]
print(sum(number) % 10000)

Python: # -*- coding: utf-8 -*-
"""
Pasta
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0568

"""
import sys
from collections import Counter

def solve(n, decided):
    status = Counter({(0, 0, 0): 1})
    for d in range(1, n+1):
        u = Counter()
        pastas = range(1, 4)
        if decided and decided[-1][0] == d:
            pastas = [decided.pop()[1]]
        for k, v in status.items():
            _, d2, d3 = k
            for p in pastas:
                if not d2 == d3 == p:
                    u[(d2, d3, p)] += v
        status = Counter({k: v % 10000 for k, v in u.items()})
    return sum(status.values()) % 10000


def main(args):
    n, k = map(int, input().split())
    decided = sorted([list(map(int, input().split())) for _ in range(k)], reverse=True)
    ans = solve(n, decided)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Python: N,K=map(int,input().split())
S=[0]*N
for _ in[0]*K:
 a,b=map(int,input().split())
 S[a-1]=b-1
dp=[0]*9
st,nd=S[:2]
if st+1:
 if nd+1:dp[st*3+nd]=1
 else:dp[st*3:st*3]=[1]*3
elif nd+1:dp[nd::3]=[1]*3
else:dp=[1]*9
for i in range(2,N):
 cur=S[i];tmp=[0]*9
 if cur+1:
  for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 else:
  for cur in range(3):
   for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 dp=tmp[:]
print(sum(dp)%10000)

Python: N,K=map(int,input().split())
dp=[[[0]*3 for j in range(2)] for i in range(N+1)]
#dp[i][j][k] iコメまででj+1回連続でkを食べる組み合わせ
G=[-1]*(N+1)
INF=10000
for i in range(K):
    a,b=map(int,input().split())
    b-=1
    G[a]=b
if G[1]!=-1:
    dp[1][0][G[1]]=1
else:
    dp[1][0][0]=dp[1][0][1]=dp[1][0][2]=1
for i in range(2,N+1):
    if G[i]!=-1:
        dp[i][0][G[i]]=(dp[i-1][0][(G[i]+1)%3]+dp[i-1][0][(G[i]+2)%3]+dp[i-1][1][(G[i]+1)%3]+dp[i-1][1][(G[i]+2)%3])%INF
        dp[i][1][(G[i])]=(dp[i-1][0][(G[i])])%INF
        continue
    for k in range(3):
        dp[i][0][k]=(dp[i-1][0][(k+1)%3]+dp[i-1][0][(k+2)%3]+dp[i-1][1][(k+1)%3]+dp[i-1][1][(k+2)%3])%INF
        dp[i][1][k]=(dp[i-1][0][k])%INF
ans=0
for j in range(2):
    for k in range(3):
        ans+=dp[N][j][k]
        ans%=INF
print(ans)

Python: N,K=map(int,input().split())
S=[-1]*N
for _ in[0]*K:a,b=map(int,input().split());S[a-1]=b-1
dp=[0]*9
st,nd=S[:2]
if st+1:
 if nd+1:dp[st*3+nd]=1
 else:dp[st*3:st*4]=[1]*3
elif nd+1:dp[nd::3]=[1]*3
else:dp=[1]*9
for i in range(2,N):
 cur=S[i];tmp=[0]*9
 if cur+1:
  for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 else:
  for cur in range(3):
   for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 dp=tmp[:]
print(sum(dp)%10000)

Python: N,K=map(int,input().split())
S=[0]*N
for _ in[0]*K:
 a,b=map(int,input().split())
 S[a-1]=b
print(S)
dp=[0]*9
st,nd=S[:2]
if st:
 if nd:dp[~-st*3+~-nd]=1
 else:dp[~-st*3:st*3]=[1]*3
elif nd:dp[~-st::3]=[1]*3
else:dp=[1]*9
for i in range(2,N):
 cur=S[i];tmp=[0]*9
 if cur:
  cur-=1
  for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 else:
  for cur in range(3):
   for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 dp=tmp[:]
print(sum(dp)%10000)