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)
}

C++: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
long long n, k,dp[101][4][4][4];
long long a[101];
long long solve(int i, int m1, int m2) {
	long long sum = 0;
	if (i == n+1) return 1;
	for (int j = 1; j <= 3; j++) {
		if ((!(j == m1&&j == m2) || (a[i + 1] == m1))
			&&!(a[i]==m1&&a[i]==m2)&&(a[i]==-1||a[i]==j)) {
			if (dp[i][j][m1][m2] != -1) {
				sum += (dp[i][j][m1][m2]%10000);
			}
			else {
				sum += ((dp[i][j][m1][m2] = solve(i + 1, j, m1))%10000);
			}
		}
	}
	return (sum%10000);
}
int main() {
	while (cin >> n >> k) {
		for (int i = 0; i <= n; i++) a[i] = -1;
		for (int i = 0; i < k; i++) {
			int b, c;
			cin >> b >> c;
			a[b] = c;
		}
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j < 4; j++) {
				for (int x = 0; x < 4; x++) {
					for (int y = 0; y < 4; y++) dp[i][j][x][y] = -1;
				}
			}
		}
		long long sum = 0;
		for (int i = 1; i <= 3; i++) {
			if (a[1] == -1||a[1]==i) {
				for (int j = 1; j <= 3; j++) {
					if ((a[2] == -1) || (a[2] == j)) {
						sum += (solve(3, j, i)%10000);
					}
				}
			}
		}
		cout << sum%10000 << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int N, K;
    cin >> N >> K;
    vector<int> pasta(N + 1, -1);
    rep(i, K) {
        int a, b;
        cin >> a >> b;
        b--;
        pasta[a] = b;
    }

    // dp[i][j][k] = pasta j (0-indexed) at day i (1-indexed), pasta k at day i - 1
    VVV dp(N + 1, VV(3, V(3, 0)));
    dp[0][0][0] = 1;
    const int mod = 10000;
    rep2(i, 1, N + 1) {
        rep(j, 3) {
            if (pasta[i] != -1 && j != pasta[i]) continue;
            rep(k, 3) {
                rep(l, 3) {
                    if (i >= 3 && j == k && k == l) continue;
                    (dp[i][j][k] += dp[i - 1][k][l]) %= mod;
                }
            }
        }
    }

    int ans = 0;
    rep(j, 3) {
        rep(k, 3) {
            (ans += dp[N][j][k]) %= mod;
        }
    }

    cout << ans << endl;

}

C++: //============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

// 1. dp?????????
// ????????\????????\
//??????????????????????????????
//??????????????????????????????
//??????????????????????????????
//??????????????????????????????
//??????????????????????????????
//??????????????????????????????
//??????????????????????????????
//??????????????????????????????
//??????????????????????????????
int dp[10][101];
// 2. n?????????
int n,k;

// 7. calc?????????
int calc(int i,int m){
	// 8. dp???????????°??????
	if(dp[i][m]>=0)return dp[i][m];
	// 9. dp????¨????

	if(i==1)dp[i][m]=calc(4,m-1)+calc(7,m-1);
	if(i==2 || i==3)dp[i][m]=calc(1,m-1)+calc(4,m-1)+calc(7,m-1);
	if(i==5)dp[i][m]=calc(2,m-1)+calc(8,m-1);
	if(i==4 || i==6)dp[i][m]=calc(2,m-1)+calc(5,m-1)*calc(8,m-1);
	if(i==9)dp[i][m]=calc(3,m-1)+calc(6,m-1);
	if(i==7 || i==8)dp[i][m]=calc(3,m-1)+calc(6,m-1)+calc(9,m-1);
	dp[i][m]=dp[i][m]%10000;
	return dp[i][m];
}
int main() {
	cin>>n>>k;

	//3.?????????
	for(int i=0;i<10;i++){
		for(int j=0;j<101;j++){
			dp[i][j]=-1;
		}
	}
	//4.??\???
	int a,b;
	for(int i=1;i<k+1;i++){
		cin>>a>>b;
		if(b==1){
			dp[2][a]=0;
			dp[3][a]=0;
			dp[5][a]=0;
			dp[6][a]=0;
			dp[8][a]=0;
			dp[9][a]=0;
		}
		if(b==2){
			dp[1][a]=0;
			dp[3][a]=0;
			dp[4][a]=0;
			dp[6][a]=0;
			dp[7][a]=0;
			dp[9][a]=0;
		}
		if(b==3){
			dp[1][a]=0;
			dp[2][a]=0;
			dp[4][a]=0;
			dp[5][a]=0;
			dp[7][a]=0;
			dp[8][a]=0;
		}
	}

	//5.???????????¨???
	if(dp[1][1]<0 && dp[1][2]<0)dp[1][2]=1;
	if(dp[1][1]<0 && dp[2][2]<0)dp[2][2]=1;
	if(dp[1][1]<0 && dp[3][2]<0)dp[3][2]=1;
	if(dp[2][1]<0 && dp[4][2]<0)dp[4][2]=1;
	if(dp[2][1]<0 && dp[5][2]<0)dp[5][2]=1;
	if(dp[2][1]<0 && dp[6][2]<0)dp[6][2]=1;
	if(dp[3][1]<0 && dp[7][2]<0)dp[7][2]=1;
	if(dp[3][1]<0 && dp[8][2]<0)dp[8][2]=1;
	if(dp[3][1]<0 && dp[9][2]<0)dp[9][2]=1;

	//6.??????
	int total=0;
	for(int i=1;i<10;i++){
		total+=calc(i,n);
	}

	total=total%10000;

	cout<<total<<endl;


	return 0;
}