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())
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+1)
 else:
  for cur in range(3):
   for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur+1)
 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
dp=[0]*9
st,nd=S[:2]
if st:
 if nd:dp[st*2+~-nd]=1
 else:dp[st*2:st*3]=[1]*3
elif nd:dp[~-nd::3]=[1]*3
else:dp=[1]*9
for i in range(2,N):
 cur=S[i]-1;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: #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 = 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

for s in schedule[2:]:
    tmp = [0]*9
    for i in range(9):
        l1 = i // 3
        l2 = 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)]
    for i in range(9):
         number[i] = sum(tmp[i])
print(sum(number) % 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
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[~-nd::3]=[1]*3
else:dp=[1]*9
for i in range(2,N):
 cur=S[i]-1;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())
rules = [map(int, input().split()) for _ in range(k)]

ans = [[0,1,1,1,1,1,1] for _ in range(n+1)]

dic = [0, [0,1,0,0,1,0,0], [0,0,1,0,0,1,0], [0,0,0,1,0,0,1]]
for [d, p] in rules:
    ans[d] = list(dic[p])

ans[1][4] = 0
ans[1][5] = 0
ans[1][6] = 0

for d in range(2, n+1):
    ans[d][1] = ans[d][1] * (ans[d-1][2] + ans[d-1][3] + ans[d-1][5] + ans[d-1][6])
    ans[d][2] = ans[d][2] * (ans[d-1][1] + ans[d-1][3] + ans[d-1][4] + ans[d-1][6])
    ans[d][3] = ans[d][3] * (ans[d-1][1] + ans[d-1][2] + ans[d-1][4] + ans[d-1][5])
    ans[d][4] = ans[d][4] *  ans[d-1][1]
    ans[d][5] = ans[d][5] *  ans[d-1][2]
    ans[d][6] = ans[d][6] *  ans[d-1][3]

print(sum(ans[n]) % 10000)

C++: //02
#include<iostream>

using namespace std;

int main(){
  int n,k;
  cin>>n>>k;
  int p[101]={};
  while(k--){
    int a,b;
    cin>>a>>b;
    p[a]=b;
  }
  int v[101][4][4]={{{}}};
  int ct;
  for(int i=1;i<=n;i++){
    ct=0;
    for(int j=1;j<=3;j++){
      if(!p[i]||j==p[i]){
	if(i==1){
	  v[1][j][0]=1;
	  ct++;
	}else{
	  for(int k=1;k<=3;k++){
	    if(i==2){
	      v[2][j][k]+=v[1][k][0];
	      ct+=v[1][k][0];
	    }else{
	      for(int l=1;l<=3;l++){
		if(!(j==k&&k==l)){
		  v[i][j][k]=(v[i][j][k]+v[i-1][k][l])%10000;
		  ct=(ct+v[i-1][k][l])%10000;
		}
	      }
	    }
	  }
	}
      }
    }
  }
  cout<<ct<<endl;
  return 0;
}

C++: #include<cstdio>

using namespace std;
int d[128];
int dp[128][3][2];

int main(){
    int n,k;
    scanf("%d%d",&n,&k);

    int a,b;
    for(int i = 0;i < k; i++){
        scanf("%d%d",&a,&b);
        d[a] = b;
    }

    for(int i = 0;i < 3; i++){
        dp[1][i][0] = 1;
    }

    for(int i = 1;i <= n; i++){
        for(int j = 0;j < 3; j++){
                if(d[i] > 0 && d[i] != j+1){
                    dp[i][j][0] = 0;
                    dp[i][j][1] = 0;
                }
                else {
                    for(int x = 0;x < 3; x++){
                        if(x != j){
                            dp[i][j][0] += dp[i-1][x][0] + dp[i-1][x][1];
                        }
                    }
                    dp[i][j][1] += dp[i-1][j][0];//‘O&#8218;&#204;“&#250;&#8218;&#204;“&#252;&#8218;&#234;&#8218;&#198;&#8218;&#173;
                    dp[i][j][0] %= 10000;
                    dp[i][j][1] %= 10000;
                    }
                }
            }
        int ans=0;
        for(int i = 0;i < 3; i++){
            ans += dp[n][i][0];
            ans += dp[n][i][1];
        }
        /*for(int i = 0;i <= n; i++){
            for(int j = 0;j < 3; j++){
                printf("%5d",dp[i][j][0]);
            }
            puts("");
        }*/
        printf("%d\n",ans%10000);
    return 0;
}