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>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
テつ?
int dp[3][110][2], num[110];
int n,k;
テつ?
int main(){
テつ?つ?つ?つ?ep(i,110) num[i] = -1;
テつ?つ?つ?つ?in >> n >> k;
テつ?つ?つ?つ?ep(i,k){
テつ?つ?つ?つ?つ?つ?つ?つ?nt a,b;
テつ?つ?つ?つ?つ?つ?つ?つ?in >> a >> b;
テつ?つ?つ?つ?つ?つ?つ?つ?--; b--;
テつ?つ?つ?つ?つ?つ?つ?つ?um[a] = b;
テつ?つ?つ?つ?
テつ?つ?つ?つ?f(num[0] == -1) rep(i,3) dp[i][0][0] = 1;
テつ?つ?つ?つ?lse dp[num[0]][0][0] = 1;
テつ?つ?つ?つ?or(int i = 1; i < n; i++){
テつ?つ?つ?つ?つ?つ?つ?つ?ep(j,3){
テつ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?f(num[i] != -1 && num[i] != j) continue;
テつ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?p[j][i][1] += dp[j][i-1][0];
テつ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?p[j][i][1] %= 10000;
テつ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?ep(u,3){
テつ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?f(j == u) continue;
テつ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?p[j][i][0] += dp[u][i-1][0]+dp[u][i-1][1];
テつ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?p[j][i][0] %= 10000;
テつ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?
テつ?つ?つ?つ?つ?つ?つ?つ?
テつ?つ?つ?つ?
テつ?つ?つ?つ?つ?つ?つ?つ?*
テつ?つ?つ?つ?ep(i,n){
テつ?つ?つ?つ?つ?つ?つ?つ?ep(j,3){
テつ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?ep(u,2){
テつ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?out << dp[j][i][u] << " ";
テつ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?
テつ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?つ?out << endl;
テつ?つ?つ?つ?つ?つ?つ?つ?
テつ?つ?つ?つ?つ?つ?つ?つ?out << endl;
テつ?つ?つ?つ?
テつ?つ?つ?つ?/
テつ?つ?つ?つ?nt ans = 0;
テつ?つ?つ?つ?ep(i,3) rep(j,2) ans += dp[i][n-1][j];
テつ?つ?つ?つ?out << ans%10000 << endl;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define MOD 10000
int n,m,dp[150][4][4],plan[150];
int rec(int n,int a,int b)
{
	if(n==0)return 1;
	if(dp[n][a][b]>=0)return dp[n][a][b];
	int res=0;
	if(plan[n]!=0)
	{
		if(a==b&&b==plan[n])return 0;
		else return rec(n-1,b,plan[n]);
	}
	if(a==b&&a!=0)
	{
		for(int i=1;i<=3;i++)
		{
			if(a!=i)
			{
				res+=rec(n-1,b,i);
				res%=MOD;
			}
		}
		return dp[n][a][b]=res;
	}
	else
	{
		for(int i=1;i<=3;i++)
		{
			res+=rec(n-1,b,i);
			res%=MOD;
		}
		return dp[n][a][b]=res;
	}
}
int main()
{
	cin >> n >> m;
	for(int i=0;i<m;i++)
	{
		int d,k;
		cin >> d >> k;
		plan[d]=k;
	}
	memset(dp,-1,sizeof(dp));
	cout << rec(n,0,0) << endl;
	return 0;
}

C++: #include <queue>
#include <iostream>
using namespace std;

int h,i,j,n,k,b,*p,y[81][5],u;
queue<int> q,w,e,r,t;

int main(){
	cin >> n >> k;
	p = new int[n+4];
	
	for(i=0; i<n+4; i++){
		p[i] = 0;
	}
	
	for(i=0; i<k; i++){
		cin >> b;
		cin >> p[b+1];
	}
	
	q.push(1);
	w.push(0);
	e.push(0);
	r.push((p[3])?p[3]:0);
	t.push((p[4])?p[4]:0);
	
	for(i=2; i<n+2; i++){
		b=0;
		for(; q.size(); ){
			for(h=1; h<4; h++){
				if(p[i]){
					y[b][0] = q.front();
					y[b][1] = e.front();
					y[b][2] = p[i];
					y[b][3] = t.front();
					y[b++][4] = (p[i+3])?p[i+3]:0;
					break;
				}else if(!((w.front() == h && e.front() == h) || (e.front() == h && h == r.front()) || (h == r.front() && h == t.front()))){
					y[b][0] = q.front();
					y[b][1] = e.front();
					y[b][2] = h;
					y[b][3] = t.front();
					y[b++][4] = (p[i+3])?p[i+3]:0;
				}
			}
			q.pop();
			w.pop();
			e.pop();
			r.pop();
			t.pop();
		}
		for(j=0; j<b; j++){
			if(y[j][0] != -1){
				for(h=0; h<b; h++){
					if((j != h)&&(y[j][1] == y[h][1])&&(y[j][2] == y[h][2])&&(y[j][3] == y[h][3])&&(y[j][4] == y[h][4])){
						y[j][0] += y[h][0];
						y[h][0] = -1;
					}
				}
				q.push(y[j][0]);
				w.push(y[j][1]);
				e.push(y[j][2]);
				r.push(y[j][3]);
				t.push(y[j][4]);
			}
		}
	}
	
	for(u=0; q.size(); ){
		u +=q.front();
		q.pop();
	}
	
	cout << u%10000 << endl;
	return 0;
}