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 <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
typedef long long int lli;
using namespace std;

/*
int dp[101][4][4];
int sche[101];

int main(){
	for(int i=0; i<101; ++i){
		sche[i]=-1;
		for(int j=0; j<4; ++j){
			for(int k=0; k<4; ++ k){
				dp[i][j][k]=-1;
			}
		}
	}
	int N,K;
	cin >> N >> K;
	for(int i=0; i<K; ++i){
		int A,B;
		cin >> A >> B;
		sche[A]=B;
	}
	for(int i=1; i<2; ++i){
		if(sche[i]!=-1){
			dp[i][sche[i]][0]=1;
		}else{
			for(int j=1; j<4; ++j){
				for(int k=1; k<4; ++k){
					dp[i][j][k]=1;
				}
			}
		}
	}
	for(int i=3; i<=N; ++i){
		if(sche[i]!=-1){
			for(int j=1; j<4; ++j){
				dp[i][sche[i]][j]=1;;
			}
		} else {
			for(int j=1; j<4; ++j){
				for(int k=1; k<4; ++k){
						
				}
			}
		}
	}
	return 0;
}
*/

long long int dp[101][4][3];
int sche[101];
int main(){
	for(int i=0; i<101; ++i){
		sche[i]=-1;
		for(int j=0; j<4; ++j){
			for(int k=0; k<3; ++k){
				dp[i][j][k]=0;
			}
		}
	}
	int N,K;
	cin >> N >> K;
	for(int i=0; i<K; ++i){
		int A,B;
		cin >> A >> B;
		sche[A]=B;
	}
	if(sche[1]!=-1){
		dp[1][sche[1]][1]=1;
	} else {
		for(int i=1; i<4; ++i){
			dp[1][i][1]=1;
		}
	}
	for(int i=2; i<=N; ++i){
		if(sche[i]!=-1){
			for(int j=1; j<4; ++j){
				if(sche[i]!=j){
					/*if(dp[i][sche[i]][1]+dp[i-1][j][1]+dp[i-1][j][2]>=dp[i][sche[i]][1])*/ dp[i][sche[i]][1]+=(dp[i-1][j][1]+dp[i-1][j][2])%10000;
					//if(dp[i][sche[i]][2]+dp[i-1][j][1]+dp[i-1][j][2]>=dp[i][sche[i]][2]) dp[i][sche[i]][2]+=dp[i-1][j][1]+dp[i-1][j][2];
				}else {
					/*if(dp[i][sche[i]][2]+dp[i-1][j][1]>=dp[i][sche[i]][2])*/ dp[i][sche[i]][2]+=dp[i-1][j][1]%10000;
				}
			}
		} else	{
			for(int j=1; j<4; ++j){
				for(int k=1; k<4; ++k){
					if(j!=k){
						/*if(dp[i][j][1]+dp[i-1][k][1]+dp[i-1][k][2]>=dp[i][j][1])*/ dp[i][j][1]+=dp[i-1][k][1]+dp[i-1][k][2]%10000;
						//if(dp[i][j][2]+dp[i-1][k][1]+dp[i-1][k][2]>=dp[i][j][2]) dp[i][j][2]+=dp[i-1][k][1]+dp[i-1][k][2];
					} else{
						/*if(dp[i][j][2]+dp[i-1][k][1]>=dp[i][j][2])*/dp[i][j][2]+=dp[i-1][k][1]%10000;
					}
				}
			}
		}
	}
	long long int ans=0;
	for(int i=1;i<4; ++i){
		for(int j=1; j<3; ++j){
			//cout << dp[N][i][j] << "\n";
			/*long long int tmp=ans;
			tmp+=dp[N][i][j];
			ans=tmp%10000;*/
			ans+=dp[N][i][j]%10000;
		}
	}
	cout << ans%10000 << "\n";
	return 0;
}

C++: #include <cstdio>

int n,k[101]={0},mem[101][4][4]={0},kd,ks,kn;

int check(int m,int d1,int d2){
    if(m>n) return 1;
    if(mem[m][d1][d2]) return mem[m][d1][d2];
    
    if(d1==d2 && d1==k[m] && d1) return 0;
    
    if(k[m])
        mem[m][d1][d2] = check(m+1,d2,k[m])%10000;
    else
        for(int i=1;i<=3;i++)
            if(d1!=d2 || d1!=i)
                mem[m][d1][d2] += check(m+1,d2,i)%10000;
    
    return mem[m][d1][d2];
}

int main(){
    scanf("%d%d",&n,&kn);
    
    for(int i=0;i<kn;i++){
        scanf("%d%d",&kd,&ks);
        k[kd]=ks;
    }
    
    printf("%d\n",check(1,0,0)%10000);
    
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int a[101] = { 0 };
	int n;
	int i;
	cin >> n >> i;
	for (int v = 0; v < i; v++){
		int y, x;
		cin >> y >> x;
		a[y] = { x };

	}
	int f;
	if (a[1] = a[2] != a[3]){
		if (a[1] != 0){
			f = 2;
		}
		if (a[1] = 0){
			f = 8;
		}
	}
	if (a[1] = a[3] != a[2]){
		if (a[1] != 0)
			f = 2;
		if (a[1] = 0){
			f = 8;
		}
	}
	if (a[3] = a[2] != a[1]){
		if (a[2] != 0)
			f = 2;
		if (a[2] = 0){
			f = 8;
		}
	}
	if (a[1] = a[2] = a[3]){
		if (a[1] = 0){
			f = 24;
		}
		if (a[1] != 0){
			f = 1;
		}

	}

	for (int v = 4; v <= n; v++){
		if (a[v] = 0){
			if (a[v - 2] = a[v - 1] != 0){
				f = f * 2;
			}
			if (a[v - 2] != a[v - 1] != 0){
				f = f * 3;
			}
			if (a[v - 2] = 0 != a[v - 1]){
				f = f * 3;
			}
			if (a[v - 1] = 0 != a[v - 2]){
				f = f * 3;
			}
		
		}
	}
	        cout << f << endl;
        	return 0;
}

C++: #include<iostream>
using namespace std;

int main(){
  int m[100];
  int n,k,a,b;

  cin >> n >> k;

  for(int i=0;i<n;i++)m[i] = -1;
  for(int i=0;i<k;i++){
    cin >> a >> b;
    m[a-1] = b-1;
  }

  int dp[101][3][3];
  for(int i=0;i<n;i++)
    for(int j=0;j<3;j++)
      for(int k=0;k<3;k++)dp[i][j][k] = 0;
  
  for(int i=0;i<n;i++){
    if(i==0){
      if(m[i]<0){
	for(int j=0;j<3;j++)dp[i][j][(j+1)%3] = 1;
      }else{
	dp[i][m[i]][(m[i]+1)%3] = 1;
      }
    }else{
      for(int j=0;j<3;j++){
	for(int k=0;k<3;k++){
	  if(m[i]<0){
	    for(int l=0;l<3;l++){
	      if(l==k && k==j)continue;
	      else {
	      dp[i][l][j] += dp[i-1][j][k]; 
	      dp[i][l][j] %= 10000;
	      }
	    }
	  }else{
	    if(!(j==m[i] && k==m[i]) ){
	      dp[i][m[i]][j] += dp[i-1][j][k];
	      dp[i][m[i]][j] %= 10000;
	    }
	  }
	}
      }
    }
  }
  /*
  for(int i=0;i<n;i++){
    for(int j=0;j<3;j++)for(int k=0;k<3;k++)cout << dp[i][j][k] << " ";
    cout << endl;
  }
  */
  int ans = 0;
  for(int i=0;i<3;i++)
    for(int j=0;j<3;j++){
      ans += dp[n-1][i][j];
      ans %= 10000;
    }

  cout << ans << endl;
}