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<cstdio>
using namespace std;

int d[100], n, k, dp[100][6];
// 0:aa, 1:bb, 2:cc, 3:a, 4:b, 5:c

int main() {
  int a, b;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) d[i] = 0;
  for (int i = 0; i < k; i++) scanf("%d%d", &a, &b), d[a-1] = b;
  dp[0][0] = dp[0][1] = dp[0][2] = dp[0][3] = dp[0][4] = dp[0][5] = 0;
  if (d[0] == 0) dp[0][3] = dp[0][4] = dp[0][5] = 1;
  if (d[0] == 1) dp[0][3] = 1;
  if (d[0] == 2) dp[0][4] = 1;
  if (d[0] == 3) dp[0][5] = 1;
  for (int i = 1; i < n; i++) {
    dp[i][0] = dp[i-1][3];
    dp[i][1] = dp[i-1][4];
    dp[i][2] = dp[i-1][5];
    dp[i][3] = dp[i-1][1] + dp[i-1][2] + dp[i-1][4] + dp[i-1][5];
    dp[i][4] = dp[i-1][0] + dp[i-1][2] + dp[i-1][3] + dp[i-1][5];
    dp[i][5] = dp[i-1][0] + dp[i-1][1] + dp[i-1][3] + dp[i-1][4];
    for (int j = 0; j < 6; j++) dp[i][j] = dp[i][j] % 10000;
    if (d[i] == 0) continue;
    if (d[i] == 1) dp[i][1] = dp[i][2] = dp[i][4] = dp[i][5] = 0;
    if (d[i] == 2) dp[i][0] = dp[i][2] = dp[i][3] = dp[i][5] = 0;
    if (d[i] == 3) dp[i][0] = dp[i][1] = dp[i][3] = dp[i][4] = 0;
  }
  int ans = 0;
  for (int j = 0; j < 6; j++) ans += dp[n-1][j];
  printf("%d\n", ans % 10000);
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

const int MAXN = 101, MAXK = 0, T = 1, C = 2, B = 3, TT = 4, CC = 5, BB = 6;

int dp[MAXN][7], UB = 10000;
int sou[MAXN];

int dif1[] = {0, C, B, T, CC, BB, TT};
int dif2[] = {0, B, T, C, BB, TT, CC};

int main()
{
    memset(dp, 0, sizeof(dp));
    memset(sou, 0, sizeof(sou));


    int n, k;
    cin >> n >> k;
    int a, b;
    REP(k){ cin >> a >> b; sou[a - 1] = b;}

    if(sou[0] == 0) dp[0][T] = dp[0][C] = dp[0][B] = 1;
    else dp[0][sou[0]] = 1;

    REP(n)
    {
        if(sou[i+1] == 0)
        {
            dp[i+1][T] = (dp[i][B] + dp[i][BB] + dp[i][C] + dp[i][CC]) % UB;
            dp[i+1][C] = (dp[i][B] + dp[i][BB] + dp[i][T] + dp[i][TT]) % UB;
            dp[i+1][B] = (dp[i][T] + dp[i][TT] + dp[i][C] + dp[i][CC]) % UB;
            dp[i+1][TT] = dp[i][T];
            dp[i+1][CC] = dp[i][C];
            dp[i+1][BB] = dp[i][B];
        }
        else
        {
            dp[i+1][sou[i+1]] = 
                (dp[i][dif1[sou[i+1] ] ] + dp[i][dif2[sou[i+1] ] ] 
                 + dp[i][dif1[sou[i+1] + 3]] + dp[i][dif2[sou[i+1] + 3]] ) % UB;
            dp[i+1][sou[i+1] + 3] = dp[i][sou[i+1]];
        }
    }
    int ans = 0;
    REP(6) ans = (ans + dp[n - 1][i + 1] ) % UB;
    //rep(j,n)REP(6) cout<<j<< " thday "  << i <<"th sourse " << dp[j][i+1] << endl;
    cout << ans << endl;

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<climits>
#include<set>
#include<tuple>
#include<stack>


using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tpl;
int dx[4] = { 1,0,0,-1 };
int dy[4] = { 0,1,-1,0 };

#define ARRAY_MAX 305

const ll INF = 1e9 + 7;
const ll MOD = 10000;

ll dp[105][5][5];
//dp[i][j][k]:=i日目の時点でパスタjをk日連続で食べているときの翌日以降の組合せ
ll N, K;
ll plan[105];

ll dfs(ll day,ll pasta,ll cnt){

	//本日の日付day,本日食べるパスタpasta、現在の連続数cnt

	if (day == N)
	{
		//最後まで問題がなかったので解として適する
		return 1LL;
	}

	if (dp[day][pasta][cnt] != -1)
	{
		//既に調べた
		return dp[day][pasta][cnt];
	}

	ll ret = 0;

	if (plan[day + 1] > 0)//翌日の予定が決まっている場合
	{
		if (pasta == plan[day + 1])//今日食べるパスタと翌日のパスタが同じ種類の場合
		{
			if (cnt == 2)//既に本日の時点で2日連続で食べている場合は不適
			{
				return 0LL;
			}
			else
			{
				//本日の時点で2連続はしていないのでまだ食べられる
				//パスタplan[day]を連続して食べるのでcntを1つ増やす
				ret += dfs(day + 1, plan[day + 1], cnt + 1);
				ret %= MOD;
			}
		}
		else
		{
			//違うパスタを食べるのでカウントをリセットする
			ret += dfs(day + 1, plan[day + 1], 1);
			ret %= MOD;
		}
	}
	else
	{
		//予定がない場合
		for (int i = 1; i <= 3; i++)//全てのパスタを試す
		{
			if (pasta == i)//今日食べるパスタと翌日のパスタが同じ種類の場合
			{
				if (cnt == 2)//既に本日の時点で2日連続で食べている場合は調べない
				{
					continue;
				}
				else
				{
					//2日連続でまだ食べていないのでまだ食べられる
					ret += dfs(day + 1, i, cnt + 1);
					ret %= MOD;
				}
			}
			else
			{
				//違う種類のパスタを食べる
				ret += dfs(day + 1, i, 1);
				ret %= MOD;
			}
		}
	}
	return dp[day][pasta][cnt] = (ret % MOD);
}

int main() {

	cin >> N >> K;
	for (int i = 0; i < K; i++)
	{
		ll a, b;
		cin >> a >> b;
		plan[a] = b;//a日目はbを食べる
	}

	for (int i = 0; i < 105; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			for (int k = 0; k < 5; k++)
			{
				dp[i][j][k] = -1;
			}
		}
	}

	ll ans = 0;
	if (plan[1] > 0)
	{
		//初日に既に予定が決まっている場合
		ans = dfs(1, plan[1], 1);
	}
	else
	{
		//初日の予定がない場合は全部調べる
		for (int i = 1; i <= 3; i++)
		{
			ans += dfs(1, i, 1);
			ans %= MOD;
		}
	}
	cout << ans << endl;

	return 0;
}