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>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define TO_STRING(a) #a
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }
template<class T> void MSG(T a){ cout << TO_STRING(a) << " = " << a << endl; }


int dp[101][3][2];
int field[101];

const int MOD = 10000;

int rec(pint b, int no, int lim)
{
	if(no == lim)
		return 1;
	
	int res = 0;
	
	if(field[no] == 4)
	{
		rep(i, 3)
		{
			if(!(b.first == b.second && b.second == i))
			{
				pint buf = b;
				swap(b.first, b.second);
				b.second = i;
				res = (res + rec(buf, no+1, lim)) % MOD;
			}
		}
	}
	else
	{
		if((b.first == b.second && b.second == field[no]))
			return 0;
		
		swap(b.first, b.second);
		b.second = field[no];
		
		return rec(b, no+1, lim);
	}
	
	return res % MOD;
}




int main()
{
	const int INF = 1 << 30;
	
	memset(dp, 0, sizeof(dp));
	rep(i, 101) field[i] = 15;
	
	int n, k;
	cin >> n >> k;
	
	rep(i, k)
	{
		int p, s;
		cin >> p >> s;
		
		field[p-1] = s-1;
	}
	
	// initialize
	if(field[0] != 15)
	{
		dp[0][field[0]][0] = 1;
	}
	else
	{
		rep(i, 3)
		dp[0][i][0] = 1;
	}
	
	REP(i, 1, n)
	{
		if(field[i] != 15)
		{
			dp[i][field[i]][0] = (dp[i-1][(field[i] + 1)%3][0] + dp[i-1][(field[i] + 1)%3][1] + dp[i-1][(field[i] + 2)%3][0] + dp[i-1][(field[i] + 2)%3][1]) % MOD;
			dp[i][field[i]][1] = dp[i-1][field[i]][0];
		}
		else
		{
			rep(j, 3)
			{
				dp[i][j][0] += (dp[i-1][(j+1)%3][0] + dp[i-1][(j+1)%3][1] + dp[i-1][(j+2)%3][0] + dp[i-1][(j+2)%3][1]) % MOD;
				dp[i][j][1] += dp[i-1][j][0];
			}
		}
	}
	
	int res = 0;
	rep(i, 3) rep(j, 2) res += dp[n-1][i][j];
	
	cout << res % MOD << endl;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#define int long long
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)

using namespace std;

int n, m;
int k[110];
int d, t;
int sum;
int kid;
int memo[101][4][4];
int solve(int day,int p,int q) {
	//cout << q << " " << p << " " << k[day + 1] << " " << k[day + 2] << endl;
	if (day == n)return 1;
	if (memo[day][p][q]!=-1)return memo[day][p][q];
	if (!k[day]) {
	if ((q == k[day+1] || k[day+1] ==  k[day + 2])&&p!=0&&k[day+1]!=0&&k[day+2]!=0) {
			return 0;
	}
		else return memo[day][p][q]=solve(day + 1, 1, p)
			+ solve(day + 1, 2, p)
			+ solve(day + 1, 3, p);
	}
	else if (k[day]) {
		return memo[day][p][q]=solve(day + 1, k[day], p);
	}
}

int dp[101][4];

signed main() {
	cin >> n >> m;
	REP(i, 101)REP(j, 4)REP(k, 4)memo[i][j][k] = -1;
	REP(i, m) {
		cin >> d >> t;
		k[d] = t;
	}
	dp[0][1] = 1;
	dp[0][2] = 1;
	dp[0][3] = 1;
	int cnt1 = 0;
	int cnt2 = 0;
	int cnt3 = 0;
	/*for (int day = 0; day<= n; day++) {
		for (int kin = 0; kin <= 3; kin++) {
			if (day != 1 && kin == 0)continue;
			/*if (cnt1 == 2 && k[day] == 1)continue;
			if (cnt2 == 2 && k[day] == 2)continue;
			if (cnt3 == 2 && k[day] == 3)continue;
			if (kin == 1)cnt1++;
			if (kin == 2)cnt2++;
			if (kin == 3)cnt3++;
			if (k[day] == k[day + 1] && k[day] == k[day + 2]&&k[day]!=0)continue;
			cout << "[" << dp[day][kin] << "]" << endl;
			if (day >= 1){
				cout << k[day - 1] << " " << kin << " " << k[day + 1] << endl;
			}
			if ((k[day - 1] == 1 && k[day + 1] == 1 && kin == 1) || k[day - 2] == k[day] && k[day] == kin&&kin == 1) {
				dp[day + 1][kin] += dp[day][2] + dp[day][3];
				continue;
			}
			if ((k[day - 1] == 2 && k[day + 1] == 2 && kin == 2) || k[day - 2] == k[day] && k[day] == kin&&kin == 2) {
				dp[day + 1][kin] += dp[day][1] + dp[day][3];
				continue;
			}
			if ((k[day - 1] == 3 && k[day + 1] == 3 && kin == 3) || k[day - 2] == k[day] && k[day] == kin&&kin == 3) {
				dp[day + 1][kin] += dp[day][1] + dp[day][2];
				continue;
			}
				if (!k[day]) {
				if (cnt1 == 2 || (cnt1 == 2 && k[day + 1] == 1)) {
					dp[day + 1][kin] += dp[day][2] + dp[day][3];
					cnt1 = 0;
					continue;
				}
				else if (cnt2 == 2 || (cnt2 == 2 && k[day + 1] == 2)) {
					dp[day + 1][kin] += dp[day][1] + dp[day][3];
					cnt2 = 0;
					continue;
				}
				else if (cnt3 == 2 || (cnt3 == 2 && k[day + 1] == 3)) {
					dp[day + 1][kin] += dp[day][1] + dp[day][2];
					cnt3 = 0;
					continue;
				}
				cnt1 = 0;
				cnt2 = 0;
				cnt3 = 0;
				dp[day + 1][kin] += dp[day][1] + dp[day][2] + dp[day][3];
			}
			else if (k[day]) {
				//cout << day << " " << kin << endl;
				dp[day + 1][kin] += dp[day][k[day]];
			}
		}
	}*/
	cout << solve(0, 0, 0)%10000 << endl;
	return 0;
}