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)
}

Python: N,K=map(int,input().split())
S=[0]*N
for _ in[0]*K:
 a,b=map(int,input().split())
 S[a-1]=b-1
dp=[0]*9
st,nd=S[:2]
if st+1:
 if nd+1:dp[st*3+nd]=1
 else:dp[st*3:-~st*3]=[1]*3
elif nd+1:dp[nd::3]=[1]*3
else:dp=[1]*9
print(dp)
for i in range(2,N):
 cur=S[i];tmp=[0]*9
 if cur+1:
  for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 else:
  for cur in range(3):
   for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 dp=tmp[:]
print(sum(dp)%10000)

Python: N,K=map(int,input().split())
S=[0]*N
for _ in[0]*K:
 a,b=map(int,input().split())
 S[a-1]=b
dp=[0]*9
st,nd=S[:2]
if st:
 if nd:dp[~-st*3+~-nd]=1
 else:dp[~-st*3:st*3]=[1]*3
elif nd:dp[~-st::3]=[1]*3
else:dp=[1]*9
for i in range(2,N):
 cur=S[i]-1;tmp=[0]*9
 if cur>0:
  for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 else:
  for cur in range(3):
   for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 dp=tmp[:]
print(sum(dp)%10000)

Python: N,K=map(int,input().split())
S=[0]*N
for _ in[0]*K:
 a,b=map(int,input().split())
 S[a-1]=b
dp=[0]*9
st,nd=S[:2]
if st:
 if nd:dp[~-st*3+~-nd]=1
 else:dp[~-st*3:st*3]=[1]*3
elif nd:dp[~-st::3]=[1]*3
else:dp=[1]*9
for i in range(2,N):
 cur=S[i];tmp=[0]*9
 if cur:
  cur-=1
  for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 else:
  for cur in range(3):
   for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 dp=tmp[:]
print(sum(dp)%10000)

Python: N,K=map(int,input().split())
S=[0]*N
for _ in[0]*K:
 a,b=map(int,input().split())
 S[a-1]=b
dp=[0]*9
st,nd=S[:2]
if st:
 if nd:dp[~-st*3+~-nd]=1
 else:dp[~-st*3:~-st*3]=[1]*3
elif nd:dp[~-nd::3]=[1]*3
else:dp=[1]*9
for i in range(2,N):
 cur=S[i]-1;tmp=[0]*9
 if cur+1:
  for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 else:
  for cur in range(3):
   for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 dp=tmp[:]
print(sum(dp)%10000)

Python: N,K=map(int,input().split())
S=[0]*N
for _ in[0]*K:
 a,b=map(int,input().split())
 S[a-1]=b
dp=[0]*9
st,nd=S[:2];st-=1
if st:
 if nd:dp[st*3+~-nd]=1
 else:dp[st*3:st*3]=[1]*3
elif nd:dp[~-nd::3]=[1]*3
else:dp=[1]*9
for i in range(2,N):
 cur=S[i]-1;tmp=[0]*9
 if cur+1:
  for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 else:
  for cur in range(3):
   for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 dp=tmp[:]
print(sum(dp)%10000)

Python: def main(dp, n, menue):
  if menue == 0: m1, m2 = 1, 2
  elif menue == 1: m1, m2 = 0, 2
  elif menue == 2: m1, m2 = 0, 1
  dp[n][menue][0] += sum(dp[n-1][m1]) + sum(dp[n-1][m2])
  dp[n][menue][1] += dp[n-1][menue][0]

def run():
  N, K = map(int, input().split())
  dp = [[[0]*2 for _ in range(3)] for n in range(N)]
  k_li = [list(map(int, input().split())) for k in range(K)]
  k_li = sorted(k_li)
  #print(k_li)
  k_i = 0
  ka, kb = k_li[0]
  if ka == 1:
    dp[0][kb-1][0] = 1
    k_i += 1
    ka, kb = k_li[k_i]
  else:
    for k in range(3):
      dp[0][k][0] = 1
      
  for n in range(1, N):
    if (n+1) == ka:
      main(dp, n, kb-1)
      if k_i < K-1:
        k_i += 	1
        ka, kb = k_li[k_i]
    else:
      for menue in range(3):
        main(dp, n, menue)
  print(sum(map(sum, dp[-1]))%10000)
  #print(dp)
      
if __name__ == '__main__':
  run()

Python: N,K=map(int,input().split())
S=[0]*N
for _ in[0]*K:
 a,b=map(int,input().split())
 S[a-1]=b-1
dp=[0]*9
st,nd=S[:2]
if st+1:
 if nd+1:dp[st*3+nd]=1
 else:dp[st*3:st*3]=[1]*3
elif nd+1:dp[nd::3]=[1]*3
else:dp=[1]*9
for i in range(2,N):
 cur=S[i];tmp=[0]*9
 if cur+1:
  for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 else:
  for cur in range(3):
   for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 dp=tmp[:]
print(sum(dp)%10000)

Python: N,K=map(int,input().split())
S=[0]*N
for _ in[0]*K:
 a,b=map(int,input().split())
 S[a-1]=b
dp=[0]*9
st,nd=S[:2];st-=1
if st+1:
 if nd:dp[st*3+~-nd]=1
 else:dp[st*3:st*3]=[1]*3
elif nd:dp[~-nd::3]=[1]*3
else:dp=[1]*9
for i in range(2,N):
 cur=S[i]-1;tmp=[0]*9
 if cur+1:
  for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 else:
  for cur in range(3):
   for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 dp=tmp[:]
print(sum(dp)%10000)

Python: N,K=map(int,input().split())
S=[0]*N
for _ in[0]*K:
 a,b=map(int,input().split())
 S[a-1]=b
print(S)
dp=[0]*9
st,nd=S[:2]
if st:
 if nd:dp[~-st*3+~-nd]=1
 else:dp[~-st*3:st*3]=[1]*3
elif nd:dp[~-st::3]=[1]*3
else:dp=[1]*9
for i in range(2,N):
 cur=S[i];tmp=[0]*9
 if cur:
  cur-=1
  for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 else:
  for cur in range(3):
   for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 dp=tmp[:]
print(sum(dp)%10**5)

Python: N,K=map(int,input().split())
S=[0]*N
for _ in[0]*K:
 a,b=map(int,input().split())
 S[a-1]=b
dp=[0]*9
st,nd=S[:2]
if st:
 if nd:dp[~-st*3+~-nd]=1
 else:dp[~-st*3:~-st*3]=[1]*3
elif nd:dp[~-nd::3]=[1]*3
else:dp=[1]*9
for i in range(2,N):
 cur=S[i]-1;tmp=[0]*9
 if cur+1:
  for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 else:
  for cur in range(3):
   for k in range(3):tmp[k*3+cur]=sum(dp[k::3])-dp[cur*4]*(k==cur)
 dp=tmp[:]
print(sum(dp)%10000)