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++: /* {{{ Shinobu kawaii */
/*
  ______   __        _                  __                 
.' ____ \ [  |      (_)                [  |                
| (___ \_| | |--.   __   _ .--.   .--.  | |.--.   __   _   
 _.____`.  | .-. | [  | [ `.-. |/ .'`\ \| '/'`\ \[  | | |  
| \____) | | | | |  | |  | | | || \__. ||  \__/ | | \_/ |, 
 \______.'[___]|__][___][___||__]'.__.'[__;.__.'  '.__.'_/ 
                                                           
*/
/* }}} */
/* include {{{ */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>
using namespace std;
/* }}} */

// #define int long long

/* cpp template {{{ */
/* short */
#define pb push_back
#define mp make_pair
#define print(x) cout << x << '\n'
#define Fi first
#define Se second

/* REPmacro */
#define REPS(i, a, n) for (int i = (a); i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = 1; i <= (n); i++)
#define DEPS(i, a, n) for (int i = (a); i >= n; i--)
#define DEP(i, n) for (int i = (n); i >= 0; i--)

/* typedef */
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vector<int> > vii;
typedef pair<int, int> pii;

/* const */
const int INF = 999999999;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
inline bool is_uruu(int y) {return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));}
inline bool inside(int y, int x, int H, int W) {return (y >= 0 && x >= 0 && y < H && x < W);}

/* }}} */

int N, K;
int pasta[128];
int dp[101][4][4];

int dfs(int now, int one, int two)
{
  if (now == N){
    return 1;
  }

  if (dp[now][one][two] != -1){
    return dp[now][one][two];
  }

  int ans = 0;
  if (pasta[now] == 0){
    RREP(i, 3){
      if (one == two && two == i) continue;
      ans += dfs(now + 1, i, one) % 10000;
    }
  } else {
    if (one == two && two == pasta[now]) return 0;
    ans = dfs(now + 1, pasta[now], one) % 10000;
  }

  return dp[now][one][two] = ans;
}

signed main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  memset(dp, -1, sizeof(dp));
  cin >> N >> K;
  REP(i, K){
    int day, taste;
    cin >> day >> taste;
    pasta[day - 1] = taste;
  }

  print(dfs(0, 0, 0) % 10000);
  
  return (0);
}

C++: #include<iostream>
#include<cstring>
#include<map>
using namespace std;
typedef long long int ll;
ll dfs(int day,int r1,int r2);
ll dp[101][4][4],n,k;
map<int,int> plan;
int main(){
	cin >> n >> k;
	for(int i=0;i<k;i++){
		int a,b; cin >> a >> b;
		a--; plan[a] = b;
	}
	memset(dp,-1,sizeof(dp));
	cout << dfs(0,0,0)%10000 << endl;
}

ll dfs(int day,int r1,int r2){
	if(day==n) return 1;
	if(dp[day][r1][r2] != -1) return dp[day][r1][r2];
	if(plan[day] != 0){
		if(r1 == r2 && r1 == plan[day]) return 0;
		else return dfs(day+1,plan[day],r1);
	}
	ll ret = 0;
	for(int i=1;i<=3;i++){
		if(r1 == r2 && r1 == i) continue;
		ret += dfs(day+1,i,r1);
	}
	return dp[day][r1][r2] = ret;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>

#define INF_LL 9000000000000000000
#define INF 2000000000

#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;

typedef long long ll;
typedef pair<int, int> PII;

class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;

public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(0);
		}
	}

	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;

		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}
};

int main(void){
	int N, K;
	map<int, int> d;
	int dp[101][3][3] = {0};
	cin >> N >> K;
	REP(i, K){
		int a, b;
		cin >> a >> b;
		d[a-1] = b;
	}
	REP(i, 3){
		REP(j, 3){
			dp[2][i][j] = 1;
		}
	}
	if(d.find(0) != d.end()){
		REP(i, 3){
			if(i != d[0]-1){
				REP(j, 3){
					dp[2][j][i] = 0;
				}
			}
		}
	}
	if(d.find(1) != d.end()){
		REP(i, 3){
			if(i != d[1]-1){
				REP(j, 3){
					dp[2][i][j] = 0;
				}
			}
		}
	}
	if(d.find(2) != d.end()){
		dp[2][d[2]-1][d[2]-1] = 0;
	}

	for(int i = 2;i < N;i++){
		REP(j, 3){ // 2??????
			REP(k, 3){ // 1?????? 
				if(d.find(i) != d.end()){
					if(!(j == k && k == d[i]-1)){
						dp[i+1][d[i]-1][k] += dp[i][k][j];
						dp[i+1][d[i]-1][k] %= 10000;
					}
				}else{
					REP(s, 3){
						if(!(j == k && k == s)){
							dp[i+1][s][k] += dp[i][k][j] ;
							dp[i+1][s][k] %= 10000;
						}
					}
				}
			}
		}
	}
	int res = 0;
	REP(i, 3){
		REP(j, 3){
			res = (res + dp[N][i][j]) % 10000;
		}
	}
	cout << res << endl;
}