CodeEff/ECCO huggingface dataset:
# Original solution
from itertools import permutations
import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
int1 = lambda x: int(x) - 1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def main():
    n,k=MI()
    ans=k*pow(k-1,n-1)
    print(ans)

main()


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n,k=MI()
    print(pow(k-1,n-1)*k)

main()
# Original solution
N, K = list(map(int, input().split()))
print((K*((K-1)**(N-1))))

# Optimized solution
N, K = list(map(int, input().split()))
print((K*(K-1)**(N-1)))
# Original solution
n,k = list(map(int, input().split()))
print(((k-1)**(n-1)*k))

# Optimized solution
s=input().split()
N=int(s[0])
K=int(s[1])
print(((K-1)**(N-1)*K))
# Original solution
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,k=list(map(int,input().split()))
    print((k*((k-1)**(n-1))))
resolve()

# Optimized solution
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n, k = list(map(int, input().split()))
    print((k * pow(k - 1, n - 1)))
resolve()
# Original solution
N,K = [int(i) for i in input().split()]

ans = K
for _ in range(N-1):
    ans *= K - 1

print(ans)

# Optimized solution
N,K = list(map(int, input().split()))

ans = K
for _ in range(N - 1):
    ans *= K - 1
print(ans)
# Original solution
import collections
 
#文字列を一文字ずつ取得したいとき
def inputStrOnebyOne():
    s = list(eval(input()))
    return s
 
#整数を一つずつリストに入れる
def inputOnebyOne_Int():
    a = list(int(x) for x in input().split())
    return a
 
def main():
    n, k = list(map(int, input().split()))
    print((k*(k-1)**(n-1)))
 
if __name__=='__main__':
    main()

# Optimized solution
n, k = list(map(int, input().split()))
print((k*(k-1)**(n-1)))
# Original solution
N, K = list(map(int, input().split()))
A = K
if N != 1:
    for i in range(N - 1):
        A *= (K - 1)
print(A)

# Optimized solution
N, K = list(map(int, input().split()))
print((K * (K - 1) ** (N - 1)))
# Original solution
n,k=list(map(int,input().split()))
a=1
f=True
for i in range(n):
  if f:
    a*=k
    f=False
  else:
    a*=k-1
print(a)

# Optimized solution
# 入力
N,K=list(map(int,input().split()))

# 出力
print((K*pow(K-1,N-1)))
# Original solution
n, k = list(map(int, input().split()))
print((k*((k-1)**(n-1))))

# Optimized solution
n, k = list(map(int, input().split()))
print((k*(k-1)**(n-1)))
# Original solution
# -*- coding: utf-8 -*-

x1,y1,x2,y2 = list(map(int, input().split()))

X = x2 - x1
Y = y2 - y1

if X > 0 and Y > 0:
    x3 = x2 - abs(Y)
    y3 = y2 + abs(X)
    x4 = x3 - abs(X)
    y4 = y3 - abs(Y)

elif X > 0 and Y < 0:
    x3 = x2 + abs(Y)
    y3 = y2 + abs(X)
    x4 = x3 - abs(X)
    y4 = y3 + abs(Y)
    
elif X < 0 and Y > 0:
    x3 = x2 - abs(Y)
    y3 = y2 - abs(X)
    x4 = x3 + abs(X)
    y4 = y3 - abs(Y)

elif X < 0 and Y < 0:
    x3 = x2 + abs(Y)
    y3 = y2 - abs(X)
    x4 = x3 + abs(X)
    y4 = y3 + abs(Y)

elif X > 0 and Y == 0:
    x3 = x2
    y3 = y2 + abs(X)
    x4 = x3 - abs(X)
    y4 = y3

elif X == 0 and Y > 0:
    x3 = x2 - abs(Y)
    y3 = y2
    x4 = x3
    y4 = y3 - abs(Y)

elif X < 0 and Y == 0:
    x3 = x2
    y3 = y2 - abs(X)
    x4 = x3 + abs(X)
    y4 = y3

elif X == 0 and Y < 0:
    x3 = x2 + abs(Y)
    y3 = y2
    x4 = x3
    y4 = y3 + abs(Y)

print((str(x3) + ' ' + str(y3) + ' ' + str(x4) + ' ' + str(y4)))

# Optimized solution
# -*- coding: utf-8 -*-

x1,y1,x2,y2 = list(map(int, input().split()))

X = x2 - x1
Y = y2 - y1

print((str(x2 - Y) + ' ' + str(y2 + X) + ' ' + str(x1 - Y) + ' ' + str(y1 + X)))
# Original solution
A, B, C, D = map(int, input().split())

def solve(A,B,C,D):
    dx,dy = C-A,D-B
    x,y = C-dy,D+dx
    ans = [x,y]
    x,y = x-dx,y-dy
    ans += [x,y]
    return ans
print(*solve(A,B,C,D),sep=' ')


# Optimized solution
A, B, C, D = map(int, input().split())

def solve(A,B,C,D):
    dx,dy = C-A,D-B
    ans = [C-dy,D+dx,A-dy,B+dx]
    return ans
print(*solve(A,B,C,D),sep=' ')
# Original solution
import sys
import bisect

N = int(sys.stdin.readline().rstrip())
S = list(sys.stdin.readline().rstrip())
Q = int(sys.stdin.readline().rstrip())

idx = {chr(ord("a") + i): set() for i in range(26)}

for i, s in enumerate(S):
    idx[s].add(i + 1)

for _ in range(Q):
    t, i, c = sys.stdin.readline().rstrip().split()
    i = int(i)
    if t == "1":
        if S[i - 1] != c:
            idx[S[i - 1]].remove(i)
            idx[c].add(i)
            S[i - 1] = c
    else:
        c = int(c)
        ans = 0
        for a, id in list(idx.items()):
            id = sorted(list(id))
            x = bisect.bisect_left(id, i)
            y = bisect.bisect_right(id, c)
            if y - x:
                ans += 1
        print(ans)

# Optimized solution
import sys
import bisect

N = int(sys.stdin.readline().rstrip())
S = list(sys.stdin.readline().rstrip())
Q = int(sys.stdin.readline().rstrip())

idx = {chr(ord("a") + i): [] for i in range(26)}

for i, s in enumerate(S):
    idx[s].append(i + 1)

for _ in range(Q):
    t, i, c = sys.stdin.readline().rstrip().split()
    i = int(i)
    if t == "1":
        if S[i - 1] != c:
            idx[S[i - 1]].remove(i)
            bisect.insort(idx[c], i)
            S[i - 1] = c
    else:
        c = int(c)
        ans = 0
        for a, id in list(idx.items()):
            x = bisect.bisect_left(id, i)
            y = bisect.bisect_right(id, c)
            if y - x:
                ans += 1
        print(ans)
# Original solution
class SegTree():
    def segfunc(self, x, y):
        return x | y
        
    def __init__(self, ide, n, init_val):
        self.ide_ele = ide
        self.num = 2**(n-1).bit_length()
        self.seg = [self.ide_ele] * 2 * self.num
        for i in range(n):
            self.seg[i+self.num-1] = init_val[i]    
        for i in range(self.num-2,-1,-1):
            self.seg[i] = self.segfunc(self.seg[2*i+1],self.seg[2*i+2]) 
    def update(self, k, x):
        k += self.num-1
        self.seg[k] = x
        while k:
            k = (k-1)//2
            self.seg[k] = self.segfunc(self.seg[k*2+1], self.seg[k*2+2])
    def query(self, p, q):
        if q<=p:
            return self.ide_ele
        p += self.num-1
        q += self.num-2
        res = self.ide_ele
        while q-p>1:
            if p&1 == 0:
                res = self.segfunc(res, self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res, self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res, self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
        return res

def main():
    n = int(eval(input()))
    s = input().rstrip()
    q = int(eval(input()))
    ans = []
    init = [set() for _ in range(n)]
    for i in range(n):
        c = s[i]
        init[i].add(c)
    seg = SegTree(set(), n, init)
    for _ in range(q):
        x, y, z = list(map(str, input().rstrip().split()))
        if x == "1":
            y = int(y)
            y -= 1
            buf = set()
            buf.add(z)
            seg.update(y, buf)
        else:
            y = int(y)
            y -= 1
            z = int(z)
            ans.append(len(seg.query(y, z)))
    for v in ans:
        print(v)

if __name__ == "__main__":
    main()

# Optimized solution
from bisect import bisect_left
def main():
    n = int(eval(input()))
    s = input().rstrip()
    q = int(eval(input()))
    ans = []
    al = [[] for _ in range(26)]
    for i in range(n):
        al[ord(s[i]) - ord("a")].append(i)
    for _ in range(q):
        x, y, z = list(map(str, input().rstrip().split()))
        if x == "1":
            y = int(y)
            y -= 1
            if s[y] == z:
                continue
            al[ord(z) - ord("a")].insert(bisect_left(al[ord(z) - ord("a")], y), y)
            al[ord(s[y]) - ord("a")].remove(y)
            s = s[:y] + z + s[y+1:]
        else:
            y = int(y)
            y -= 1
            z = int(z)
            cnt = 0
            for i in range(26):
                index = bisect_left(al[i], y)
                if index < len(al[i]) and al[i][index] < z:
                    cnt += 1
            ans.append(cnt)
    for v in ans:
        print(v)

if __name__ == "__main__":
    main()
# Original solution
from bisect import bisect_left
def main():
    n = int(eval(input()))
    s = list(input().rstrip())
    q = int(eval(input()))
    ans = []
    al = [[] for _ in range(26)]
    for i, c in enumerate(s):
        al[ord(c) - ord("a")].append(i)
    for _ in range(q):
        x, y, z = list(map(str, input().rstrip().split()))
        if x == "1":
            y = int(y) - 1
            if s[y] == z:
                continue
            p = ord(z) - ord("a")
            al[p].insert(bisect_left(al[p], y), y)
            al[ord(s[y]) - ord("a")].remove(y)
            s[y] = z
        else:
            y = int(y)
            y -= 1
            z = int(z)
            cnt = 0
            for i in range(26):
                index = bisect_left(al[i], y)
                if index < len(al[i]) and al[i][index] < z:
                    cnt += 1
            ans.append(cnt)
    for v in ans:
        print(v)

if __name__ == "__main__":
    main()

# Optimized solution
from bisect import bisect_left, insort
def main():
    n = int(eval(input()))
    s = list(input().rstrip())
    q = int(eval(input()))
    ans = []
    al = [[] for _ in range(26)]
    for i, c in enumerate(s):
        al[ord(c) - ord("a")].append(i)
    for _ in range(q):
        x, y, z = list(map(str, input().rstrip().split()))
        if x == "1":
            y = int(y) - 1
            if s[y] == z:
                continue
            p = bisect_left(al[ord(s[y])-ord("a")],y)
            al[ord(s[y])-ord("a")].pop(p)
            insort(al[ord(z)-ord("a")],y)
            s[y] = z
        else:
            y = int(y) - 1
            z = int(z)
            cnt = 0
            for i in range(26):
                index = bisect_left(al[i], y)
                if index < len(al[i]) and al[i][index] < z:
                    cnt += 1
            ans.append(cnt)
    for v in ans:
        print(v)

if __name__ == "__main__":
    main()
# Original solution
f=input
n,s,q=int(f()),list(f()),int(f())
d={chr(97+i):[] for i in range(26)}
for i in range(n):
  d[s[i]]+=[i]
from bisect import *
for i in range(q):
  a,b,c=input().split()
  b=int(b)-1
  if a=='1':
    d[s[b]].pop(bisect(d[s[b]],b)-1)
    s[b]=c
    insort(d[c],b)
  else:
    t=0
    for l in list(d.values()):
      m=bisect(l,b-1)
      if m<len(l) and l[m]<int(c): t+=1
    print(t)

# Optimized solution
f=input
n,l,q=int(f()),list(f()),int(f())
B=[[0]*-~n for i in range(26)]
def S(o,i):
  s=0
  while i: s+=B[o][i]; i-=i&-i
  return s
def A(o,i,x):
  while i<=n: B[o][i]+=x; i+=i&-i
for i in range(n): A(ord(l[i])-97,i+1,1)
for i in range(q):
  a,b,c=f().split(); b=int(b)
  if a>'1': print((sum(1 for o in range(26) if S(o,int(c))-S(o,b-1))))
  else: A(ord(l[b-1])-97,b,-1); l[b-1]=c; A(ord(c)-97,b,1)
# Original solution
f=input
n,l,q=int(f()),list(f()),int(f())
B=[[0]*-~n for i in range(26)]
def S(o,i):
  s=0
  while i: s+=B[o][i]; i-=i&-i
  return s
def A(o,i,x):
  while i<=n: B[o][i]+=x; i+=i&-i
for i in range(n): A(ord(l[i])-97,i+1,1)
for i in range(q):
  a,b,c=f().split(); b=int(b)
  if a>'1': print((sum(1 for o in range(26) if S(o,int(c))-S(o,b-1))))
  else: A(ord(l[b-1])-97,b,-1); l[b-1]=c; A(ord(c)-97,b,1)

# Optimized solution
f=input
n,s,q=int(f()),list(f()),int(f())
d={chr(97+i):[] for i in range(26)}
for i in range(n):
  d[s[i]]+=[i]
from bisect import *
for i in range(q):
  a,b,c=f().split()
  b=int(b)-1
  if a=='1':
    if s[b]==c: continue
    d[s[b]].pop(bisect(d[s[b]],b)-1)
    s[b]=c
    insort(d[c],b)
  else:
    t=0
    for l in list(d.values()):
      m=bisect(l,b-1)
      if m<len(l) and l[m]<int(c): t+=1
    print(t)
# Original solution
f=input; from bisect import bisect_left as g, insort as h
f(); s,i=list(f()),0
from collections import *
d=defaultdict(list)
for c in s: d[c]+=[i]; i+=1
for _ in range(int(f())):
  a,b,c=f().split(); b=int(b)-1
  if a>'1': print((sum(1 for l in list(d.values()) if g(l,b)<len(l) and l[g(l,b)]<int(c))))
  elif s[b]!=c: l=d[s[b]]; l.pop(g(l,b)); s[b]=c; h(d[c],b)

# Optimized solution
f=input; from bisect import bisect_left as g, insort as h
d={chr(97+i):[] for i in range(26)}
f(); s,i=list(f()),0
for c in s: d[c]+=[i]; i+=1
for _ in range(int(f())):
  a,b,c=f().split(); b=int(b)-1
  if a>'1': print((sum(1 for l in list(d.values()) if g(l,b)<len(l) and l[g(l,b)]<int(c))))
  elif s[b]!=c: l=d[s[b]]; l.pop(g(l,b)); s[b]=c; h(d[c],b)
# Original solution
# AtCoder Beginner Contest 157
# E - Simple String Queries
# https://atcoder.jp/contests/abc157/tasks/abc157_e
from bisect import bisect_left, insort_left
import sys
input = sys.stdin.readline
OFS = ord('a')
_ = eval(input())
S = list(input()[:-1])

D = [[] for _ in [0]*26]
for i, s in enumerate(S):
    D[ord(s)-OFS].append(i)

for _ in [0]*int(eval(input())):
    Q = input()[:-1].split()
    if Q[0] == '1':
        i, c = int(Q[1])-1, Q[2]

        s = ord(S[i])-OFS
        deli = bisect_left(D[s], i)
        del D[s][deli]
        insort_left(D[ord(c)-OFS], i)
        S[i] = c

    else:
        l, r = int(Q[1])-1, int(Q[2])-1

        cnt = 0
        for L in D:
            i = bisect_left(L, l)
            if i < len(L) and L[i] <= r:
                cnt += 1

        print(cnt)


# Optimized solution
# AtCoder Beginner Contest 157
# E - Simple String Queries
# https://atcoder.jp/contests/abc157/tasks/abc157_e
from bisect import bisect_left, insort_left
import sys
input = sys.stdin.readline
OFS = ord('a')
_ = eval(input())
S = list(input()[:-1])

D = [[] for _ in [0]*26]
for i, s in enumerate(S):
    D[ord(s)-OFS].append(i)

for _ in [0]*int(eval(input())):
    Q = input()[:-1].split()
    if Q[0] == '1':
        i, c = int(Q[1])-1, Q[2]

        if S[i] != c:
            s = ord(S[i])-OFS
            D[s].pop(bisect_left(D[s], i))
            insort_left(D[ord(c)-OFS], i)
            S[i] = c

    else:
        l, r = int(Q[1])-1, int(Q[2])-1

        cnt = 0
        for L in D:
            i = bisect_left(L, l)
            if i < len(L) and L[i] <= r:
                cnt += 1

        print(cnt)
# Original solution
N = int(eval(input()))
S = list(input().strip())
Qcnt = int(eval(input()))

cidx = {}
for i in range(ord("a"), ord("z")+1):
    cidx[chr(i)] = set([])

import bisect

for idx, s in enumerate(S):
    cidx[s].add(idx)

for _ in range(Qcnt):
    Q = list(input().strip().split(" "))
    if Q[0] == "1":
        q_idx = int(Q[1]) - 1
        prev_c = S[q_idx]
        S[q_idx] = Q[2]
        cidx[prev_c].remove(q_idx)
        cidx[Q[2]].add(q_idx)
    else:
        l = int(Q[1])
        r = int(Q[2])
        l -= 1
        cnt = 0
        for alpha in range(ord("a"), ord("z")+1):
            tmp = sorted(list(cidx[chr(alpha)]))
            lid = bisect.bisect_left(tmp, l)
            rid = bisect.bisect_left(tmp, r)
            if lid < rid:
                cnt += 1
        print(cnt)

# Optimized solution
class SegTree:
    def __init__(self, N, S):
        self.N = 2 ** ((N-1).bit_length())
        self.tree = [0 for i in range(self.N * 2 - 1)]
        for i in range(len(S)):
            self.tree[self.N - 1 + i] = 1 << (ord(S[i]) - ord("a"))

        for i in range(self.N - 2, -1, -1):
            self.tree[i] = self.tree[i*2 + 1] | self.tree[i*2 + 2]

    def update(self, index, char):
        i = self.N - 1 + index
        # アルファベットがある = bitが立っている
        self.tree[i] = 1 << (ord(char) - ord("a"))
        while i > 0:
            i = (i-1) // 2
            self.tree[i] = self.tree[i*2 + 1] | self.tree[i*2 + 2]

    def get(self, a, b):
        return self.query(a, b, 0, 0, self.N)

    # k : ノードの番号
    # l : ノードkの受け持つ区間
    # r : ノードkの受け持つ区間
    def query(self, a, b, k, l, r):
        if r <= a or b <= l: # 区間外
            return 0
        elif a <= l and r <= b: # 受け持ち区間は完全に含まれている
            return self.tree[k]
        else: # 一部含まれてる
            left = self.query(a, b, 2 * k + 1, l, (l+r)//2)
            right = self.query(a, b, 2 * k + 2, (l+r)//2, r)
            return left | right

def solve():
    N = int(eval(input()))
    S = list(input().strip())
    tree = SegTree(N, S)

    Q = int(eval(input()))
    for _ in range(Q):
        t_q, in_1, in_2 = input().split()
        if t_q == "1":
            i_q = int(in_1)
            c_q = in_2
            tree.update(i_q-1, c_q)
        else:
            l_q = int(in_1)
            r_q = int(in_2)
            print((bin(tree.get(l_q-1, r_q)).count("1")))

solve()
# Original solution
#!python3

import sys

from bisect import bisect, insort_left

iim = lambda: map(int, input().rstrip().split())

def resolve():
    N = int(input())
    S = list(input())
    Q = int(input())

    c0 = ord('a')
    S1 = [[] for i in range(26)]
    for i, c1 in enumerate(S):
        ci = ord(c1) - c0
        S1[ci].append(i)

    ans = []
    for cmd, i, j in (line.split() for line in sys.stdin):
        i = int(i) - 1
        if cmd == "1":
            if S[i] == j:
                continue

            c1 = ord(S[i]) - c0
            s1 = S1[c1]
            s1.remove(i)
            S[i] = j
            c1 = ord(j) - c0
            insort_left(S1[c1], i)
        elif cmd == "2":
            j = int(j) - 1
            num = 0

            for s1 in S1:
                ls = len(s1)
                k = bisect(s1, i-1, 0, ls)

                if k == ls:
                    continue
                if i <= s1[k] <= j:
                    num += 1

            ans.append(num)

    print(*ans, sep="\n")

if __name__ == "__main__":
    resolve()


# Optimized solution
#!python3

import sys

from bisect import bisect, insort_left

iim = lambda: map(int, input().rstrip().split())

def resolve():
    N = int(input())
    S = list(input())
    Q = int(input())

    c0 = ord('a')
    S1 = [[] for i in range(26)]
    for i, c1 in enumerate(S):
        ci = ord(c1) - c0
        S1[ci].append(i)

    ans = []
    for cmd, i, j in (line.split() for line in sys.stdin):
        i = int(i) - 1
        if cmd == "1":
            if S[i] == j:
                continue

            c1 = ord(S[i]) - c0
            s1 = S1[c1]
            s1.pop(bisect(s1, i)-1)
            S[i] = j
            c1 = ord(j) - c0
            insort_left(S1[c1], i)
        elif cmd == "2":
            j = int(j) - 1
            num = 0

            for s1 in S1:
                ls = len(s1)
                k = bisect(s1, i-1, 0, ls)

                if k == ls:
                    continue
                if i <= s1[k] <= j:
                    num += 1

            ans.append(num)

    print(*ans, sep="\n")

if __name__ == "__main__":
    resolve()
# Original solution
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

class SegmentTree:
    # 初期化処理
    # f : SegmentTreeにのせるモノイド
    # default : fに対する単位元
    def __init__(self, size, default=0):
        self.size = 2**(size-1).bit_length() # 簡単のため要素数Nを2冪にする
        self.default = default
        self.dat = [default]*(self.size*2) # 要素を単位元で初期化

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.dat[i*2] | self.dat[i*2+1]

    def query(self, l, r):
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = lres | self.dat[l]
                l += 1
            if r & 1:
                r -= 1
                rres = self.dat[r] | rres # モノイドでは可換律は保証されていないので演算の方向に注意
            l >>= 1
            r >>= 1
        res = lres | rres
        return len(res)

N = ir()
S = sr()
seg = SegmentTree(N, default=set())
for i in range(N):
    seg.update(i, set(S[i]))
    
Q = ir()
for _ in range(Q):
    a, b, c = sr().split()
    if a == '1':
        seg.update(int(b)-1, set(c))
    else:
        print((seg.query(int(b)-1, int(c))))

# 03

# Optimized solution
import sys
from bisect import bisect_left, bisect_right, insort

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
S = list('-' + sr())
d = [[] for _ in range(26)]
for i in range(1, N+1):
    s = S[i]
    o = ord(s) - ord('a')
    insort(d[o], i)

Q = ir()
for _ in range(Q):
    q, a, b = sr().split()
    if q == '1':
        a = int(a)
        if S[a] == b:
            continue
        prev = ord(S[a]) - ord('a')
        d[prev].pop(bisect_left(d[prev], a))
        next = ord(b) - ord('a')
        insort(d[next], a)
        S[a] = b
    else:
        a = int(a); b = int(b)
        ans = 0
        for alpha in range(26):
            if bisect_right(d[alpha], b) - bisect_left(d[alpha], a) >= 1:
                ans += 1
        print(ans)
# Original solution
# coding: utf-8
import sys
from bisect import bisect_left, bisect_right, insort_left
from collections import defaultdict

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 各アルファベットのindexのリストを作り,bisect_left()
N = ir()
S = list(sr())
dic = defaultdict(list)
for i, s in enumerate(S):
    dic[s].append(i)

Q = ir()
for _ in range(Q):
    a, b, c = sr().split()
    if a == '1':
        b = int(b) - 1
        if S[b] == c:
            continue
        dic[S[b]].remove(b)
        insort_left(dic[c], b)
        S[b] = c
    else:
        b = int(b) - 1; c = int(c) - 1
        kind = 0
        for x in range(26):
            word = chr(ord('a')+x)
            if len(dic[word]) == 0:
                continue
            i = bisect_left(dic[word], b)
            j = bisect_right(dic[word], c)
            if i != j:
                kind += 1
        print(kind)

# 07

# Optimized solution
import sys
from bisect import bisect_left, bisect_right, insort

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
S = list('-' + sr())
d = [[] for _ in range(26)]
for i in range(1, N+1):
    s = S[i]
    o = ord(s) - ord('a')
    d[o].append(i)

Q = ir()
for _ in range(Q):
    q, a, b = sr().split()
    if q == '1':
        a = int(a)
        if S[a] == b:
            continue
        prev = ord(S[a]) - ord('a')
        d[prev].remove(a)
        next = ord(b) - ord('a')
        insort(d[next], a)
        S[a] = b
    else:
        a = int(a); b = int(b)
        ans = 0
        for alpha in range(26):
            if bisect_right(d[alpha], b) - bisect_left(d[alpha], a) >= 1:
                ans += 1
        print(ans)