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
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
using namespace std;

//conversion
//------------------------------------------
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();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
#define INF 10000000


LL dp[2][3][101];//dp[連続日数][種類][日数]

int main(){
	int n,k;
	cin>>n>>k;
	CLR(dp);
	vector<int>check[101];
	REP(i,k){
		int a,b;
		cin>>a>>b;
		a--;b--;
		check[a].PB(b);
	}
	REP(i,3){
		if(!check[1].empty()&&!check[2].empty()&&check[1][0]==i&&check[2][0]==i){
			dp[0][i][0]=0;
		}else if(!check[0].empty()){
			dp[0][check[0][0]][0]=1;
		}else dp[0][i][0]=1;
	}
	FOR(i,1,n){
		if(!check[i].empty()){
			int t=check[i][0];
			REP(j,3)REP(k,2)if(t!=j)dp[0][t][i]+=dp[k][j][i-1];
			dp[1][t][i]+=dp[0][t][i-1];
			continue;
		}
			
		REP(j,3)REP(j2,3){
			if(j!=j2)REP(k,2)dp[0][j][i]+=dp[k][j2][i-1];
			else dp[1][j][i]+=dp[0][j][i-1];
		}
	}
	LL sum=0;
	REP(i,3)REP(j,2)sum+=dp[j][i][n-1];
	sum%=10000;
	cout<<sum<<endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

/* {{{ */
#define CL(N) cout << N << endl
#define DCT(N) cout << "debug " << N << endl
#define REP(i, N) for (int i = 0; i < N; i++)
/* }}} */

int K[128], menu[128][4][4];

int check(int n, int m, int d1, int d2)
{
    if (m > n) return (1);
    if (menu[m][d1][d2]) return (menu[m][d1][d2]);

    if (d1 == d2 && d1 == K[m] && d1) return (0);

    if (K[m]){
        menu[m][d1][d2] = check(n, m + 1, d2, K[m]) % 10000;
    }
    else {
        for (int i = 1; i <= 3; i++){
            if (d1 != d2 || d1 != i) menu[m][d1][d2] += check(n, m + 1, d2, i) % 10000;
        }
    }

    return (menu[m][d1][d2]);
}

int main()
{
    int n, k;

    cin >> n >> k;

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

    CL(check(n, 1, 0, 0) % 10000);

    return (0);
}

C++: //Solution for aoj 0568:Pasta
#include <iostream>
using namespace std;
const int surplus = 10000;
int n, k, a, b, ans;
int pasta[101][4][2], save[101];
void dp(int day){
	if (day > n)
		return;
	if (save[day]){
		for (int i = 1; i <= 3; i++){
			if (save[day] != i)
				pasta[day][save[day]][0] += pasta[day - 1][i][0] + pasta[day - 1][i][1];
			else
				pasta[day][i][1] += pasta[day - 1][i][0];
		}
		pasta[day][save[day]][0] %= surplus;
		pasta[day][save[day]][1] %= surplus;
	}
	else{
		for (int i = 1; i <= 3; i++){
			for (int j = 1; j <= 3; j++){
				if (i != j){
					pasta[day][i][0] += pasta[day - 1][j][0] + pasta[day - 1][j][1];
				}
				else{
					pasta[day][i][1] += pasta[day - 1][j][0];
				}
			}
			pasta[day][i][0] %= surplus;
			pasta[day][i][1] %= surplus;
		}
	}
	dp(day + 1);
}
int main(){
	cin >> n >> k;
	for (int i = 0; i < k; i++){
		cin >> a >> b;
		save[a] = b;
	}
	if (save[1])
		pasta[1][save[1]][0] = 1;
	else{
		for (int i = 1; i <= 3; i++){
			pasta[1][i][0] = 1;
		}
	}
	dp(2);
	for (int i = 1; i <= 3; i++){
		ans += pasta[n][i][0] + pasta[n][i][1];
	}
	ans %= surplus;
	cout << ans << endl;
	return 0;
}

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

int n, k, next;
int day[101];
int dp[101][4][4];
int solve(int i, int j, int k);

int main(void){
	fill_n(**dp,101*4*4,-1);
	fill_n(day, 101, 0);
	cin >> n >> k;
	for(int i = 0, a; i < k; i++){
		cin >> a;
		cin >> day[a];
	}
	cout << solve(0, 0, 0) << endl;
}

int solve(int i, int b1, int b2){
	if(i == n) return 1;
	if(dp[i][b1][b2] != -1) return dp[i][b1][b2];
	if(day[i] != 0){
		if(b1 == b2 && b1 == day[i]){return 0;}
		else return solve(i+1, day[i], b1)%10000;
	}
	int sum = 0;
	for(int next = 1; next <= 3; next++){
		if(b1 == b2 && b1 == next)continue;
		sum += solve(i+1, next, b1)%10000;
	}
	return dp[i][b1][b2] = sum%10000;
}