import sys input = sys.stdin.readline N,Q=map(int,input().split()) C=[[0,0],[0,1],[0,2],[0,3],[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]] LIST=[[-1]*4 for i in range(4)] for i in range(len(C)): x,y=C[i] LIST[x][y]=i def seg_function(x,y): # Segment treeで扱うfunction #print(x,y) ANS=[1<<30]*10 for i in range(10): for j in range(10): a,b=C[i] c,d=C[j] if b<=c: k=LIST[a][d] ANS[k]=min(ANS[k],x[i]+y[j]) return ANS seg_el=1<<(N.bit_length()) # Segment treeの台の要素数 SEG=[[1<<30]*10 for i in range(2*seg_el)] # 1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化 def update(n,x,seg_el): # A[n]をxへ更新 i=n+seg_el SEG[i]=x i>>=1 # 子ノードへ while i!=0: SEG[i]=seg_function(SEG[i*2],SEG[i*2+1]) i>>=1 def getvalues(l,r): # 区間[l,r)に関するseg_functionを調べる L=l+seg_el R=r+seg_el ANS1=[0]*10 ANS2=[0]*10 while L>=1 R>>=1 return seg_function(ANS1, ANS2) S=input().strip() AX=[] for i in range(4): X=[1]*10 for j in range(i+1): for k in range(i,4): X[LIST[j][k]]=0 #print(X) AX.append(X) for i in range(len(S)): update(i,AX[ord(S[i])-65],seg_el) for tests in range(Q): x,y,z=input().split() if int(x)==1: y=int(y)-1 update(y,AX[ord(z)-65],seg_el) else: ANS=getvalues(int(y)-1,int(z)) print(min(ANS))