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<stdio.h>
#include<math.h>
#include<algorithm>
#include<cctype>
#include<string.h>
#include <stdlib.h>

#define PI 3.1415926
void swap(int* x, int* y) {
	int a = *x;
	int b = *y;
	*x = b;
	*y = a;
}
int factorial(int n);
int sum(int n, int*);
int ave(int n, int*);
int min(int n, int*), min(int n, int m);
int max(int n, int*), max(int n, int m);

int main() {
	int N, K, B[200] = { 0 },A;
	int X[200][10] = { 0 }, Y[10] = { 0 };
	scanf("%d %d", &N, &K);
	for (int i = 1; i <= K; i++) {
		scanf("%d", &A);
		scanf("%d", &B[A]);
	}
	/*
	for (int i = 1; i <= N; i++) {
		printf("%d %d\n", i, B[i]);
	}
	*/
	
	if (B[1] == 0) {
		X[1][1] = 1;
		X[1][2] = 1;
		X[1][3] = 1;
	}
	else if(B[1] == 1) {
		X[1][1] = 1;
	}
	else if (B[1] == 2) {
		X[1][2] = 1;
	}
	else if (B[1] == 3) {
		X[1][3] = 1;
	}
	if (B[2] == 0) {
		X[2][1] = X[1][1] + X[1][2] + X[1][3];
		X[2][2] = X[1][1] + X[1][2] + X[1][3];
		X[2][3] = X[1][1] + X[1][2] + X[1][3];
	}
	else if (B[2] == 1) {
		X[2][1] = X[1][1] + X[1][2] + X[1][3];;
	}
	else if (B[2] == 2) {
		X[2][2] = X[1][1] + X[1][2] + X[1][3];;
	}
	else if (B[2] == 3) {
		X[2][3] = X[1][1] + X[1][2] + X[1][3];;
	}
	for (int i = 3; i <= N; i++) {
		//printf("%d %d\n", i, B[i]);
		if (B[i] == 0) {
			X[i][1] = X[i - 1][2] + X[i - 1][3] + X[i - 2][2] + X[i - 2][3];
			X[i][2] = X[i - 1][3] + X[i - 1][1] + X[i - 2][3] + X[i - 2][1];
			X[i][3] = X[i - 1][1] + X[i - 1][2] + X[i - 2][1] + X[i - 2][2];
			if (X[i - 1][1] == 0) {
				X[i][1] -= X[i - 2][2] + X[i - 2][3];
			}
			if (X[i - 1][2] == 0) {
				X[i][2] -= X[i - 2][3] + X[i - 2][1];
			}
			if (X[i - 1][3] == 0) {
				X[i][3] -= X[i - 2][1] + X[i - 2][2];
			}
		}
		else if (B[i] == 1) {
			X[i][1] = X[i - 1][2] + X[i - 1][3] + X[i - 2][2] + X[i - 2][3];
			X[i][2] = 0;
			X[i][3] = 0;
			if (X[i - 1][1] == 0) {
				X[i][1] -= X[i - 2][2] + X[i - 2][3];
			}
		}
		else if (B[i] == 2) {
			X[i][1] = 0;
			X[i][2] = X[i - 1][3] + X[i - 1][1] + X[i - 2][3] + X[i - 2][1];
			X[i][3] = 0;
			if (X[i - 1][2] == 0) {
				X[i][2] -= X[i - 2][3] + X[i - 2][1];
			}
		}
		else if (B[i] == 3) {
			X[i][1] = 0;
			X[i][2] = 0;
			X[i][3] = X[i - 1][1] + X[i - 1][2] + X[i - 2][1] + X[i - 2][2];
			if (X[i - 1][3] == 0) {
				X[i][3] -= X[i - 2][1] + X[i - 2][2];
			}
		}
		X[i][1] %= 10000;
		X[i][2] %= 10000;
		X[i][3] %= 10000;
	}
	/*
	for (int i = 1; i <= N; i++) {
		printf("%d %d %d\n", X[i][1], X[i][2], X[i][3]);
	}
	*/
	printf("%d\n", (X[N][1] + X[N][2] + X[N][3])%10000);
	return 0;
}

int factorial(int n) {
	if (n > 0) {
		return n * factorial(n - 1);
	}
	else {
		return 1;
	}
}
int sum(int n, int *all) {
	int total = 0;
	for (int i = 0; i < n; ++i) {
		total += all[i];
	}
	return total;
}
int ave(int n, int *all) {
	int total = 0;
	for (int i = 0; i<n; ++i) {
		total += all[i];
	}
	return total / n;
}
int min(int n, int *all) {

	int min = all[0];
	for (int i = 1; i < n; ++i) {
		if (all[0] > all[i]) {
			min = all[i];
		}
	}
	return min;
}
int max(int n, int *all) {
	int max = all[0];
	for (int i = 1; i<n; ++i) {
		if (all[0]<all[i]) {
			max = all[i];
		}
	}
	return max;
}
int max(int n, int m) {
	if (n >= m) {
		return n;
	}
	else {
		return m;
	}
}
int min(int n, int m) {
	if (n >= m) {
		return m;
	}
	else {
		return n;
	}
}