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 dp[100][4][4];
int plan[100], N, K;

int solve(int today, int before1, int before2) {
  if( today == N ) return 1;
  if( dp[today][before1][before2] != -1 ) 
    return (dp[today][before1][before2]);
  if( plan[today] ) {
    if( plan[today] == before1 && before1 == before2 ) {
      return 0;
    } 
    else {
      return (solve(today+1, plan[today], before1));
    }
  }

  int ret = 0;
  for(int i=1; i<=3; i++) {
    if( i != before1 || before1 != before2 ) {
      ret += solve(today+1, i, before1);
    }
  }

  return dp[today][before1][before2] = ret;
} 
	      


int main() {
  cin >> N >> K;
  memset(plan, 0, sizeof(plan));
  memset(dp, -1, sizeof(dp));

  for(int i=0; i<K; i++) {
    int a, b;
    cin >> a >> b;
    plan[a-1] = b;
  }

  cout << solve(0,0,0) % 10000 << endl;
}

C++: #include<stdio.h>
int dp[101][4][4];
int main(void)
{
	int n,d;
	int a,b;
	int i,j,k,l;
	int s=0;
	int pst[101];
	
	scanf("%d %d",&n,&d);
	for(i=1;i<=n;i++){
		pst[i]=0;
		for(j=1;j<=3;j++){
			for(k=1;k<=3;k++){
				dp[i][j][k]=0;
			}
		}
	}
	for(i=0;i<d;i++){
		scanf("%d %d",&a,&b);
		pst[a]=b;
	}
	if(pst[1]==0){
		for(i=1;i<=3;i++){
			for(j=1;j<=3;j++){
				dp[1][i][1]=1;
			}
		}
	}
	else {
		for(i=1;i<=3;i++){
			dp[1][pst[1]][1]=1;
		}
	}
	for(i=1;i<=3;i++){
		if(pst[1]==i || pst[1]==0){
			for(j=1;j<=3;j++){
				if(pst[2]==j || pst[2]==0){
					dp[2][j][i]+=1;
				}
			}
		}
	}
	for(i=2;i<n;i++){
		for(j=1;j<=3;j++){
			if(pst[i]==j || pst[i]==0){
				for(k=1;k<=3;k++){
					for(l=1;l<=3;l++){
						if(pst[i+1]==l || pst[i+1]==0){
							if(!(j==k && j==l)){
									dp[i+1][l][j]+=dp[i][j][k];
									dp[i+1][l][j]=dp[i+1][l][j]%10000;
							}
						}
					}
				}
			}
		}
	}
	for(i=1;i<=3;i++){
		for(j=1;j<=3;j++){
			s+=dp[n][i][j];
			s=s%10000;
		}
	}
	printf("%d\n",s);
	return 0;
}

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

#define REP(i, s, n) for(int i = (s); i < int(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(co) co.begin(), co.end()

int dp[101][3][2];
int sche[101];
const int MOD = 10000;

int main()
{
	int n, k; cin >> n >> k;
	n--;
	memset(sche, -1, sizeof(sche));
	rep(i, k)
	{
		int a, b; cin >> a >> b;
		a--; b--;
		sche[a] = b;
	}

	if (~sche[0]) dp[0][sche[0]][0] = 1;
	else rep(i, 3) dp[0][i][0] = 1;

	rep(i, n)
	{
		if (~sche[i + 1]) rep(j, 3)
		{
			if (j == sche[i + 1]) dp[i + 1][sche[i + 1]][1] += dp[i][sche[i + 1]][0] % MOD;
			else dp[i + 1][sche[i + 1]][0] += (dp[i][j][0] + dp[i][j][1]) % MOD;
		}
		else rep(j, 3) rep(k, 3)
		{
			if (j == k) dp[i + 1][j][1] += dp[i][k][0] % MOD;
			else dp[i + 1][j][0] += (dp[i][k][0] + dp[i][k][1]) % MOD;
		}
	}

	int ans = 0;
	rep(i, 3) (ans += (dp[n][i][0] + dp[n][i][1])) %= MOD;
	cout << ans << endl;
	
	return 0;
}

C++: // tsukasa_diary's programing contest code template
#include <bits/stdc++.h>
using namespace std;
// define
#define for_(i,a,b) for(int i=a;i<b;++i)
#define for_rev(i,a,b) for(int i=a;i>=b;--i)
#define allof(a) a.begin(),a.end()
#define minit(a,b) memset(a,b,sizeof(a))
#define size_of(a) (int)a.size()
// typedef
typedef long long lint;
typedef double Double;
typedef pair<int, int> pii;
//
typedef vector< int > Array;
typedef vector< Array > Matrix;
typedef vector< Double > DArray;
typedef vector< DArray > DMatrix;
typedef vector< string > SArray;
typedef vector< pii > PArray;
// popcount
inline int POPCNT(int _x) { return __builtin_popcount(_x); }
inline int POPCNT(lint _x) { return __builtin_popcountll(_x); }
// inf
const int iINF = 1L << 30;
const lint lINF = 1LL << 60;
// eps
Double EPS = 1e-9;
// in range
inline bool in_range(int _v, int _mx, int _mi) { return _mi <= _v && _v < _mx; }
inline bool in_range(Double _v, Double _mi, Double _mx) { return -EPS < _v - _mi && _v - _mx < EPS; }
inline bool in_range(int _x, int _y, int _W, int _H) { return 0 <= _x && _x < _W && 0 <= _y && _y < _H; }
// neighbor clockwise
const int DX[4] = {0,1,0,-1}, DY[4] = {-1,0,1,0};
const int DX_[8] = {0,1,1,1,0,-1,-1,-1}, DY_[8] = {-1,-1,0,1,1,1,0,-1};

int N, K;
int pasta[110];

int dp[110][5][5];
const int MOD = 1e4;

void modAdd(int& dpi, int& val) {
	dpi += val;
	dpi %= MOD;
}

void solve() {
	minit(dp, 0);
	
	if (pasta[0] != -1) dp[1][pasta[0]][1] = 1;
	else for_(j,0,3) dp[1][j][1] = 1;
	
	for_(i,1,N) for_(j,0,3) for_(k,1,3) {
		if (pasta[i] == -1) {
			for_(jj,0,3) {
				if (jj == j) modAdd(dp[i + 1][jj][k + 1], dp[i][j][k]);
				else modAdd(dp[i + 1][jj][1], dp[i][j][k]);
			}
		} else {
			if (j == pasta[i]) modAdd(dp[i + 1][pasta[i]][k + 1], dp[i][j][k]);
			else modAdd(dp[i + 1][pasta[i]][1], dp[i][j][k]);
		}
	}
	
	int ans = 0;
	for_(j,0,3) for_(k,0,3) modAdd(ans, dp[N][j][k]);
	
	cout << ans << endl;
}

int main() {
	cin >> N >> K;
	
	minit(pasta, -1);
	for_(i,0,K) {
		int a, b; cin >> a >> b;
		--a; --b;
		pasta[a] = b;
	}
	
	solve();
	return 0;
}