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<algorithm>
#include<vector>
#define A first
#define B second

using namespace std;

typedef pair<int,int> P;

int main(void){
	
	int n,k,dp[102][4][3];
	
	cin >> n >> k;
	
	vector<P>v(k);
	
	for(int i=0;i<k;i++)
		cin >> v[i].A >> v[i].B;
	
	sort(v.begin(),v.end());
	
	for(int i=0;i<101;i++)
		for(int j=0;j<4;j++)
			for(int k=0;k<3;k++)
				dp[i][j][k]=0;
	
		for(int i=1;i<4;i++)dp[1][i][1]=1;
	
	int p=0;
	
	for(int i=1;i<=n;i++){
		for(int k=1;k<4;k++){
			for(int l=1;l<3;l++){
					
				if(i==v[p].A){
					if(v[p].B==k && l<2)dp[i+1][k][l+1]+=dp[i][v[p].B][l]%10000;
					else if(v[p].B!=k)dp[i+1][k][1]+=dp[i][v[p].B][l]%10000;
					continue;
				}
					
				for(int j=1;j<4;j++){
					if(j==k && l<2)dp[i+1][k][l+1]+=dp[i][j][l]%10000;
					else if(j!=k)dp[i+1][k][1]+=dp[i][j][l]%10000;
				}
			}
		}
		if(v[p].A==i)p++;
	}
	
	int ans=0;
	
	if(n==v[p-1].A){
		for(int j=1;j<3;j++){
			ans+=dp[n][v[p-1].B][j];
			ans%=10000;
		}
	}
	else {
		for(int i=1;i<4;i++){
			for(int j=1;j<3;j++){
				ans+=dp[n][i][j];
				ans%=10000;
			}
		}
	}
	
	cout << ans << endl;
	
return 0;
}

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

enum Sauce{NOT_DETERMINED=0, Tomato=1, Cream=2, Basil=3};
enum Seq{SAME, DIFF};

const int divisor = 10000;

class Pasta{
public:
	Pasta(int _N);
	~Pasta();

	Sauce* sauceary;
	int schedule(int i, Sauce yesterday, Seq seq);

private:
	int N;
	int** memo;
};

Pasta::Pasta(int _N){
	N = _N;
	sauceary = new Sauce[N+1];
	memo = new int*[N+1];

	for (int i = 0; i <= N; i++) sauceary[i] = NOT_DETERMINED;

	for (int i = 0; i <= N; i++){
		memo[i] = new int[4];
		for (int j = 0; j < 4; j++) memo[i][j] = -1;
	}
}

Pasta::~Pasta(){
	delete[] sauceary;
	delete[] memo;
}

int Pasta::schedule(int i, Sauce yesterday, Seq seq){
	if (i == N+1) return 1;
	if (seq == SAME && yesterday == sauceary[i]) return 0;

	if (sauceary[i] == NOT_DETERMINED){
		switch (yesterday){
		case Tomato: 
			if (seq == SAME) return (schedule(i+1, Cream, DIFF) + schedule(i+1, Basil, DIFF)) % divisor;
			else if (seq == DIFF) return (schedule(i+1, Tomato, SAME) + schedule(i+1, Cream, DIFF) + schedule(i+1, Basil, DIFF)) % divisor;
		case Cream:
			if (seq == SAME) return (schedule(i+1, Tomato, DIFF) + schedule(i+1, Basil, DIFF)) % divisor;
			else if (seq == DIFF) return (schedule(i+1, Tomato, DIFF) + schedule(i+1, Cream, SAME) + schedule(i+1, Basil, DIFF)) % divisor;
		case Basil:
			if (seq == SAME) return (schedule(i+1, Tomato, DIFF) + schedule(i+1, Cream, DIFF)) % divisor;
			else if (seq == DIFF) return (schedule(i+1, Tomato, DIFF) + schedule(i+1, Cream, DIFF) + schedule(i+1, Basil, SAME)) % divisor;
		}
	}
	else {
		if (yesterday == sauceary[i]) return schedule(i+1, sauceary[i], SAME) % divisor;
		else return schedule(i+1, sauceary[i], DIFF) % divisor;
	}
}

int main(){
	int N, K;
	cin >> N >> K;

	Pasta* pasta = new Pasta(N);

	for (int i = 0; i < K; i++){
		int A, B;
		cin >> A >> B;
		pasta->sauceary[A] = (Sauce)B;
	}

	int ans;
	if (pasta->sauceary[1] == NOT_DETERMINED){
		ans = pasta->schedule(2, Tomato, DIFF) + pasta->schedule(2, Cream, DIFF) + pasta->schedule(2, Basil, DIFF);
	} else {
		ans = pasta->schedule(2, pasta->sauceary[1], DIFF);
	}

	cout << ans % divisor << endl;

	delete pasta;
	return 0;
}

C++: #include <iostream>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, m, n) for (int i = m; i < n; i++)

int main() {
    int n, k;
    std::cin >> n >> k;

    int date[100];
    int dp[100][4][4];
    rep(i, 100) {
        date[i] = 0;
        rep(a, 4) rep(b, 4) dp[i][a][b] = 0;
    }

    int d, p;
    rep(i, k) {
        std::cin >> d >> p;
        date[d - 1] = p;
    }

    if (date[0] == 0)
        dp[0][0][1] = dp[0][0][2] = dp[0][0][3] = 1;
    else
        dp[0][0][date[0]] = 1;
    repr(i, 1, n) {
        if (date[i] == 0) {
            repr(p, 1, 4) rep(a, 4) rep(b, 4) if (a != b || b != p)
                dp[i][a][p] += dp[i - 1][b][a] % 10000;
        } else {
            rep(a, 4) rep(b, 4) if (a != b || b != date[i]) dp[i][a][date[i]] +=
                dp[i - 1][b][a] % 10000;
        }
    }
    int total = 0;
    rep(a, 4) rep(b, 4) total += dp[n - 1][a][b];
    std::cout << total % 10000 << std::endl;
}

C++: #include<iostream>
#include<map>

#define MOD 10000

using namespace std;

map<int,int>data;

int dp[200][3][3]={0};

int main(){
	int n,k;
	cin>>n>>k;
	for(int i=0;i<k;i++){
		int a,b;
		cin>>a>>b;
		data[a]=b;
	}
	
	for(int i=1;i<=n;i++){
		map<int,int>::iterator pa;
		bool it=false;
		if(data.find(i)!=data.end()){
			pa=data.find(i);
			it=true;
		}
		
		if(i==1){
			if(it){
				dp[i][pa->second-1][1]=1;
			}
			else {
				for(int j=0;j<3;j++){
					dp[i][j][1]=1;
				}
			}
		}
		else {
			if(it){
				for(int j=0;j<3;j++){
					if(j!=pa->second-1)dp[i][pa->second-1][1]+=dp[i-1][j][1]+dp[i-1][j][2];
					dp[i][pa->second-1][1]%=MOD;
				}
				dp[i][pa->second-1][2]=dp[i-1][pa->second-1][1];
			}
		
			else {
				for(int k=0;k<3;k++){
					for(int j=0;j<3;j++){
						if(j!=k)dp[i][k][1]+=dp[i-1][j][1]+dp[i-1][j][2];
						dp[i][k][1]%=MOD;
					}
					dp[i][k][2]=dp[i-1][k][1];
				}
				
			}
		}
		
	}
	
	int ans=0;
	for(int i=0;i<3;i++){
		ans+=dp[n][i][1]+dp[n][i][2];
	}
	cout<<ans%MOD<<endl;
}