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++: //pasta
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<vector>
#define rep(i,a) for(int i=0;i<(a);++i)
#define repd(i,a) for(int i=(a);i>=0;--i)

int N, K;
int dp[101][3][2];//day,pastaの種類,前の日と同じかどうか
int P[101];
const int MOD = 10000;

int main(){
	scanf("%d%d", &N, &K);
	std::fill(P,P + N , -1);
	
	rep(i, K){
		int A, B;
		scanf("%d%d", &A, &B);
		P[A - 1] = B - 1;
	}
	if (~P[0])
		dp[0][P[0]][0] = 1;
	else rep(j, 3)
		dp[0][j][0] = 1;
	rep(i, N-1){
		if (P[i + 1] == -1)rep(j, 3){
			rep(k, 3) if (j != k)
				dp[i + 1][j][0] = (dp[i + 1][j][0] + dp[i][k][0] + dp[i][k][1]) % MOD;
			dp[i + 1][j][1] = (dp[i + 1][j][1] + dp[i][j][0]) % MOD;
		}
		else {
			rep(j, 3)if (j != P[i + 1])
				dp[i + 1][P[i + 1]][0] = (dp[i + 1][P[i + 1]][0] + dp[i][j][0] + dp[i][j][1]) % MOD;
			dp[i + 1][P[i + 1]][1] = (dp[i + 1][P[i + 1]][1] + dp[i][P[i + 1]][0]) % MOD;
		}
	}
	int ans = 0;
	rep(j, 3) rep(k, 2)
		ans = (ans + dp[N - 1][j][k]) % MOD;
	printf("%d\n", ans);
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int k,a,b,day[101],n, memo[101][4][4];
int dfs(int j,int d1,int d2);
int main(){
  cin >> n >> k;
  fill(day,day+101,0);
  memset(memo,-1,sizeof(memo));
  for(int i=0;i<k;i++){
    cin >> a >> b;
    day[a] = b;
  }
  cout << dfs(1,0,0) << endl;
  return (0);
}

int dfs(int j,int d1,int d2){
  int ret = 0;
  if(j == n+1) return 1;
  if(memo[j][d1][d2] != -1) return memo[j][d1][d2];
  else if(day[j] != 0){
    if(d1!=d2||d2!=day[j]) ret += dfs(j+1,d2,day[j]);
  }
  else {
    for(int i=1;i<=3;i++){
      if(d1!=d2||d2!=i) ret += dfs(j+1,d2,i);
    }
  }
  return memo[j][d1][d2] = ret%10000;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>

using namespace std;

#define rep(i,n) for(int i = 0; i<n ; i++)

#define INF 1001001001
#define LLINF 1001001001001001001LL

typedef pair<int,int> P;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

//------------------HEAD---------------------

int dp[100][3][3]; //dp[N][n-2 pasta][n-3 pasta]

int N;

int pastas[102];

int solve(int n, int p1, int p2){
    if(n==N)return 1;
    //cerr<<n<<" "<<p1<<" "<<p2<<" "<<dp[n][p1][p2]<<endl;
    if(p1!=-1&&p2!=-1&&dp[n][p1][p2]!=INF){
        return dp[n][p1][p2];
    }
    int t=0;
    if(pastas[n]!=-1){ //already decided
        if(p1!=p2||p2!=pastas[n])t=solve(n+1, p2, pastas[n]);
    }else{
        rep(i,3){
            if(p1!=p2||p2!=i)t+=solve(n+1,p2,i);
        }
    }
    t=t%10000;
    //<<t<<endl;
    dp[n][p1][p2]=t;
    return t;
}

int main(void){
    // Here your code !
    int K;
    cin>>N>>K;
    rep(i,N)pastas[i]=-1;
    rep(i,K){
        int a,b;
        cin>>a>>b;
        pastas[a-1]=b-1;
    }
    rep(i,N)rep(j,3)rep(k,3)dp[i][j][k]=INF;
    int ans=0;
    
    ans=solve(0,-1,-1);
    
    cout<<ans%10000<<endl;
    /*rep(i,N){
        rep(j,3)rep(k,3)cerr<<dp[i][j][k]<<"\t";
        cerr<<endl;
    }*/
    return 0;
}

C++: #include <iostream>
#include <functional>
#include <algorithm>
#include <climits>
#include <string>
#include <vector>
#include <map>
#include <cmath>

using namespace std;

namespace _{struct _{_(){ // initialize
  cin.tie(0); ios::sync_with_stdio(false);
}}_;}
template <typename T, size_t size>
inline size_t len(const T (&)[size]) { return size; }
#define println(s) cout << s << '\n';
#define str to_string
#define rep(i, n) for (int i=0; i<int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_N 110

int N, K;
int table[MAX_N];
// offset: 0 ~ MAX_N
// A, B: 0 ~ 2
int cache[MAX_N][3][3];

bool invalid(int x) {
  return x != 0 && x != 1 && x != 2;
}

bool isConst(int offset) {
  return !invalid(table[offset]);
}

// (offset) A B ? ...
int solve(int offset, int A, int B) {
  // cout << "solve(" << offset <<", " << A <<", " << B <<")" << '\n';
  // | x y ? [wall]
  if (offset + 2 >= N) return 1; // goal
  if (offset >= 0 && cache[offset][A][B] != -1) return cache[offset][A][B];

  int ans = -1;
  // A B C ...
  // shift
  if (isConst(offset+2)) {
    int next = table[offset+2];
    // check
    if (A == B && B == next) return 0;
    ans = solve(offset+1, B, table[offset+2]);
  }

  // A B ? ...
  else {
    // x x [^x] ...
    if (A == B) {
      int a, b = -1;
      if (A == 0) { a = 1; b = 2; }
      if (A == 1) { a = 0; b = 2; }
      if (A == 2) { a = 0; b = 1; }
      ans = solve(offset+1, B, a) + solve(offset+1, B, b);
    }
    // x y (any) ...
    else {
      ans = solve(offset+1, B, 0) + solve(offset+1, B, 1) + solve(offset+1, B, 2);
    }
  }
  ans = ans % 10000;
  if (offset >= 0) cache[offset][A][B] = ans;
  return ans;
}



int main() {
  cin >> N >> K;
  rep(i, MAX_N) table[i] = -1;
  rep(i, MAX_N)
    rep(a, 3)
      rep(b, 3)
        cache[i][a][b] = -1;
  // input
  rep(i, K) {
    int a, b;
    cin >> a >> b;
    table[a-1] = b-1;
  }

  // rep(i, N) cout << " "<<(table[i]==-1?"?":str(table[i]));
  // main
  cout << solve(-2, -11, -12) << '\n';
  return 0;
}