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 <bits/stdc++.h>

#define LL long long
#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VL vector<LL>
#define VVL vector<vector<LL>>
#define VVVL vector<vector<vector<LL>>>
#define VB vector<bool>
#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

template<class T> T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> void print(T x) { std::cout << x << std::endl; }

const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy = { 0, -1, 0, 1 }, dx = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）

using namespace std;

const int MOD = 10000;

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

    int N, K;
    cin >> N >> K;
    map<int, int> m;
    FOR(i, 0, K) {
        int a, b;
        cin >> a >> b;
        m[a] = b;
    }

    VVVL dp(N + 1, VVL(4, VL(4, 0)));
    if (m[1]) {
        dp[1][0][m[1]] = 1;
    }
    else {
        dp[1][0][1] = 1;
        dp[1][0][2] = 1;
        dp[1][0][3] = 1;
    }

    FOR(i, 1, N) {
        FOR(j, 0, 4) {
            FOR(k, 0, 4) {

                if (m[i + 1]) {
                    if (j == k and k == m[i + 1]) {
                    }
                    else {
                        (dp[i + 1][k][m[i + 1]] += dp[i][j][k]) %= MOD;
                    }
                    continue;
                }

                if (j == k) {
                    if (j == 1) {
                        (dp[i + 1][k][2] += dp[i][j][k]) %= MOD;
                        (dp[i + 1][k][3] += dp[i][j][k]) %= MOD;
                    }
                    else if (j == 2) {
                        (dp[i + 1][k][1] += dp[i][j][k]) %= MOD;
                        (dp[i + 1][k][3] += dp[i][j][k]) %= MOD;
                    }
                    else if (j == 3) {
                        (dp[i + 1][k][1] += dp[i][j][k]) %= MOD;
                        (dp[i + 1][k][2] += dp[i][j][k]) %= MOD;
                    }

                }
                else {
                    (dp[i + 1][k][1] += dp[i][j][k]) %= MOD;
                    (dp[i + 1][k][2] += dp[i][j][k]) %= MOD;
                    (dp[i + 1][k][3] += dp[i][j][k]) %= MOD;
                }
            }
        }
    }

    LL ans = 0;
    FOR(j, 0, 4) {
        FOR(k, 0, 4) {
            (ans += dp[N][j][k]) %= MOD;
        }
    }
    print(ans % MOD);
}

C++: #include<iostream>
#include<cstring>
#include<vector>

using namespace std;

int solve(int,int,int);

int n,k,cnt;
int d[102];
int memo[102][4][4];

int main(){
	cin >> n >> k;
	
	for(int i=0;i<k;i++){
		int tmp;
		cin >> tmp;
		cin >> d[tmp];
	}
	cout << solve(1,0,0) << endl;
	return 0;
}

int solve(int day,int old_type,int type){
	int rec = 0;
	if(memo[day][old_type][type] != 0)return memo[day][old_type][type];
	
	if(day == n+1){
		rec = 1;
	}
	else if(d[day] != 0){
		if(old_type == type && type == d[day])rec = 0;
		else rec = solve(day+1,type,d[day]);
	}
	else{
		for(int i=1;i<=3;i++){
			if(old_type == type && type == i)continue;
			rec += solve(day+1,type,i);
		}
	}
	return memo[day][old_type][type] = rec%10000;
}

C++: #include<iostream>
using namespace std;
int n,k;
int dayp[101]={0};
long long int mem[4][101][4][4]={0};
long long int pasta(int x, int y, int a,int kioku); 
int main(){
  int j;
  cin >> n >> k;
  for(int i=0;i<k;i++){
    cin >> j;
    cin >> dayp[j];
  }
  if(dayp[1]!=0){
    cout<<pasta(dayp[1],1,1,0)%10000<<endl;
  }
  else
    cout << (pasta(1,1,1,0)+pasta(2,1,1,0)+pasta(3,1,1,0))%10000 << endl;
  return 0;
}
long long int pasta(int x,int y, int a,int kioku){
  if(dayp[y+1]!=0){
    if(kioku==x){
      a++;
    }
    else if(a>1){
      a=1;
    }
    if(a==3){
      return 0;
    } 
    if(y==n){
      return 1;
    }
    if(mem[x][y][a][kioku]!=0){
      return mem[x][y][a][kioku];
    }
    return mem[x][y][a][kioku]=pasta(dayp[y+1],y+1,a,x);
  }
  else{
    if(kioku==x){
      a++;
    }
    else if(a>1){
      a=1;
    }
    if(a==3){
      return 0;
    } 
    if(y==n){
      return 1;
    }
    if(mem[x][y][a][kioku]!=0){
      return mem[x][y][a][kioku];
    }
    return mem[x][y][a][kioku]=pasta(1,y+1,a,x)+pasta(2,y+1,a,x)+pasta(3,y+1,a,x);
  }
}