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

int main() {
	int N, K;
	cin >> N >> K;
	int A[K], B[K];
	for (int i = 0; i < K; i++) cin >> A[i] >> B[i];
	int dp[N][9];
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < 9; j++) {
			dp[i][j] = 0;
		}
	}
	bool jud[N][9];
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < 9; j++) {
			jud[i][j] = true;
		}
	}
	for (int i = 0; i < K; i++) {
		int x = A[i], y = B[i];
		if (y == 1) {
			jud[x - 1][1] = false;
			jud[x - 1][2] = false;
			jud[x - 1][4] = false;
			jud[x - 1][5] = false;
			jud[x - 1][7] = false;
			jud[x - 1][8] = false;
		}
		else if (y == 2) {
			jud[x - 1][0] = false;
			jud[x - 1][2] = false;
			jud[x - 1][3] = false;
			jud[x - 1][5] = false;
			jud[x - 1][6] = false;
			jud[x - 1][8] = false;
		}
		else {
			jud[x - 1][0] = false;
			jud[x - 1][1] = false;
			jud[x - 1][3] = false;
			jud[x - 1][4] = false;
			jud[x - 1][6] = false;
			jud[x - 1][7] = false;
		}
	}

	for (int i = 0; i < 9; i++) {
		if (jud[1][i]) {
			if (0 <= i && i <= 2) {
				if (jud[0][0] || jud[0][3] || jud[0][6]) {
					dp[1][i] = 1;
				}
			}
			else if (3 <= i && i <= 5) {
				if (jud[0][1] || jud[0][4] || jud[0][7]) {
					dp[1][i] = 1;
				}
			}
			else {
				if (jud[0][2] || jud[0][5] || jud[0][8]) {
					dp[1][i] = 1;
				}
			}
		}
	}

	for (int i = 2; i < N; i++) {
		for (int j = 0; j < 9; j++) {
			if (jud[i][j]) {
				if (j == 0) {
					dp[i][j] = dp[i - 1][3] + dp[i - 1][6];
				}
				else if (j == 1 || j == 2) {
					dp[i][j] = dp[i - 1][0] + dp[i - 1][3] + dp[i - 1][6];
				}
				else if (j == 3 || j == 5) {
					dp[i][j] = dp[i - 1][1] + dp[i - 1][4] + dp[i - 1][7];
				}
				else if (j == 4) {
					dp[i][j] = dp[i - 1][1] + dp[i - 1][7];
				}
				else if (j == 6 || j == 7) {
					dp[i][j] = dp[i - 1][2] + dp[i - 1][5] + dp[i - 1][8];
				}
				else {
					dp[i][j] = dp[i - 1][2] + dp[i - 1][5];
				}
				dp[i][j] %= 10000;
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < 9; i++) {
		ans += dp[N - 1][i];
		ans %= 10000;
	}
	cout << ans << endl;
}

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

int N, K, A, B;

int dfs(vector<bool>& dec, vector<int>& pasta,int n)
{
	int res = 0;
	if (n == N + 1)
		return 1;
	if (dec[n]) {
		if (n - 2 >= 0 && pasta[n - 2] == pasta[n - 1] && pasta[n - 1] == pasta[n])
			return 0;
		else
			return dfs(dec, pasta, n + 1);
	}
	for (int i = 1; i <= 3; i++) {
		pasta[n] = i;
		if (n - 2 >= 0 && pasta[n - 2] == pasta[n - 1] && pasta[n - 1] == pasta[n])
			continue;
		res += dfs(dec, pasta, n + 1);
	}
	return res;
}

int main()
{
	cin >> N >> K;
	vector<bool> dec(N + 1);
	vector<int> pasta(N + 1);
	for (int i = 0; i < K; i++) {
		cin >> A >> B;
		dec[A] = 1;
		pasta[A] = B;
	}
	cout << dfs(dec, pasta,1) % 10000 << endl;
	return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,n-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);

typedef vector<vvi> vvvi;

int main() {
	int N, K;

	while(cin >> N >> K, N) {
		vvvi dp(N+1, vvi(4, vi(4, 0)));
		map<int, int> info;

		REP(i, K) {
			int a, b;
			cin >> a >> b;
			info[a] = b;
		}

		if(info[1] && info[2]) {
			dp[2][info[1]][info[2]] = 1;
		} else if(info[1]) {
			FOR(i, 1, 3) {
				dp[2][info[1]][i] = 1;
			}
		} else if(info[2]) {
			FOR(i, 1, 3) {
				dp[2][i][info[2]] = 1;
			}
		} else {
		FOR(d, 1, 2) {
			FOR(i, 1, 3) {
				FOR(j, 1, 3) {
					dp[d][i][j] = 1;
				}
			}
		}
		}

		FOR(d, 3, N) {
			if(info[d]) {
				int tod = info[d];
				FOR(yes, 1, 3) {
					FOR(bef, 1, 3) {
						if(bef == yes && yes == tod) {

						} else {
							dp[d][yes][tod] += dp[d-1][bef][yes];
						}
					}
					dp[d][yes][tod] %= 10000;
				}
			} else {
				FOR(tod, 1, 3) {
					FOR(yes, 1, 3) {
						FOR(bef, 1, 3) {
							if(bef == yes && yes == tod) {

							} else {
								dp[d][yes][tod] += dp[d-1][bef][yes];
							}
						}
						dp[d][yes][tod] %= 10000;
					}
				}
			}
		}

		int ans = 0;
		FOR(i, 1, 3) {
			FOR(j, 1, 3) {
				ans += dp[N][i][j];
			}
		}

		cout << ans%10000 << endl;
	}

	return 0;
}