You have a set of $n$ discs, the $i$-th disc has radius $i$. Initially, these discs are split among $m$ towers: each tower contains at least one disc, and the discs in each tower are sorted in descending order of their radii from bottom to top.

You would like to assemble one tower containing all of those discs. To do so, you may choose two different towers $i$ and $j$ (each containing at least one disc), take several (possibly all) top discs from the tower $i$ and put them on top of the tower $j$ in the same order, as long as the top disc of tower $j$ is bigger than each of the discs you move. You may perform this operation any number of times.

For example, if you have two towers containing discs $[6, 4, 2, 1]$ and $[8, 7, 5, 3]$ (in order from bottom to top), there are only two possible operations:

  move disc $1$ from the first tower to the second tower, so the towers are $[6, 4, 2]$ and $[8, 7, 5, 3, 1]$;  move discs $[2, 1]$ from the first tower to the second tower, so the towers are $[6, 4]$ and $[8, 7, 5, 3, 2, 1]$. 

Let the difficulty of some set of towers be the minimum number of operations required to assemble one tower containing all of the discs. For example, the difficulty of the set of towers $[[3, 1], [2]]$ is $2$: you may move the disc $1$ to the second tower, and then move both discs from the second tower to the first tower.

You are given $m - 1$ queries. Each query is denoted by two numbers $a_i$ and $b_i$, and means "merge the towers $a_i$ and $b_i$" (that is, take all discs from these two towers and assemble a new tower containing all of them in descending order of their radii from top to bottom). The resulting tower gets index $a_i$.

For each $k \in [0, m - 1]$, calculate the difficulty of the set of towers after the first $k$ queries are performed.


-----Input-----

The first line of the input contains two integers $n$ and $m$ ($2 \le m \le n \le 2 \cdot 10^5$) — the number of discs and the number of towers, respectively.

The second line contains $n$ integers $t_1$, $t_2$, ..., $t_n$ ($1 \le t_i \le m$), where $t_i$ is the index of the tower disc $i$ belongs to. Each value from $1$ to $m$ appears in this sequence at least once.

Then $m - 1$ lines follow, denoting the queries. Each query is represented by two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le m$, $a_i \ne b_i$), meaning that, during the $i$-th query, the towers with indices $a_i$ and $b_i$ are merged ($a_i$ and $b_i$ are chosen in such a way that these towers exist before the $i$-th query).


-----Output-----

Print $m$ integers. The $k$-th integer ($0$-indexed) should be equal to the difficulty of the set of towers after the first $k$ queries are performed.


-----Example-----
Input
7 4
1 2 3 3 1 4 3
3 1
2 3
2 4

Output
5
4
2
0



-----Note-----

The towers in the example are:

  before the queries: $[[5, 1], [2], [7, 4, 3], [6]]$;  after the first query: $[[2], [7, 5, 4, 3, 1], [6]]$;  after the second query: $[[7, 5, 4, 3, 2, 1], [6]]$;  after the third query, there is only one tower: $[7, 6, 5, 4, 3, 2, 1]$.

Difficulty: interview

Starter code:

Solutions:
import  sys
input=sys.stdin.readline
n,m=list(map(int,input().split()))
a=[int(x) for x in input().split()]
s=[set() for i in range(m+1)]

for i in range(n):
    s[a[i]].add(i+1)

f=[0]*(m+2)
for i in range(m+1):
    f[i]=i

def fin(x):
    if f[x]==x:
        return x
    f[x]=fin(f[x])
    return f[x]
ans=0
for i in range(1,m+1):
    for j in s[i]:
        if j in s[i] and j-1 in s[i]:
            ans+=1

out=[n-ans-1]

for i in range(m-1):
    x,y=list(map(int,input().split()))
    x=fin(x)
    y=fin(y)

    if len(s[x])<len(s[y]):
        x,y=y,x
    for i in s[y]:
        if i in s[y] and i-1 in s[x]:
            ans+=1
        if i in s[y]  and i+1 in s[x]:
            ans+=1

    out.append(n-ans-1)
    s[x]|=s[y]
    f[y]=x

print('\n'.join(str(x) for x in out))




import  sys
input=sys.stdin.readline
n,m=list(map(int,input().split()))
a=[int(x) for x in input().split()]
s=[set() for i in range(m+1)]

for i in range(n):
    s[a[i]].add(i+1)

f=[0]*(m+2)
for i in range(m+1):
    f[i]=i

def fin(x):
    if f[x]==x:
        return x
    f[x]=fin(f[x])
    return f[x]
ans=0
for i in range(1,m+1):
    for j in s[i]:
        if j in s[i] and j-1 in s[i]:
            ans+=1

out=[n-ans-1]

for i in range(m-1):
    x,y=list(map(int,input().split()))
    x=fin(x)
    y=fin(y)

    if len(s[x])<len(s[y]):
        x,y=y,x
    for i in s[y]:
        if i in s[y] and i-1 in s[x]:
            ans+=1
        if i in s[y]  and i+1 in s[x]:
            ans+=1

    out.append(n-ans-1)
    s[x]|=s[y]
    f[y]=x

print('\n'.join(str(x) for x in out))




import sys
input = sys.stdin.readline

class Dsu:
    def __init__(self, _n):
        self.n = _n
        self.p = [-1] * _n

    def get(self, v):
        u = v
        while self.p[u] != -1:
            u = self.p[u]
        while self.p[v] != -1:
            nxt = self.p[v]
            self.p[v] = u
            v = nxt
        return u

    def unite(self, v, u):
        v = self.get(v)
        u = self.get(u)
        if v == u:
            return
        self.p[v] = u

n, m = map(int, input().split())
real = Dsu(m)
a = list(map(lambda x: int(x) - 1, input().split()))

idxs = [[] for i in range(n)]
for i in range(n):
    idxs[a[i]].append(i)

ans = 0
for i in range(1, n):
    if a[i] != a[i - 1]:
        ans += 1
print(ans)

for _ in range(1, m):
    v, u = map(lambda x: int(x) - 1, input().split())
    v = real.get(v)
    u = real.get(u)
    if len(idxs[v]) > len(idxs[u]):
        v, u = u, v
    real.unite(v, u)
    if v != u:
        for i in idxs[v]:
            if i >= 1 and a[i] == v and a[i - 1] == u:
                ans -= 1
            if i + 1 < n and a[i] == v and a[i + 1] == u:
                ans -= 1
        for i in idxs[v]:
            idxs[u].append(i)
            a[i] = u;
    print(ans) 

import sys
input = sys.stdin.readline

class Dsu:
    def __init__(self, _n):
        self.n = _n
        self.p = [-1] * _n

    def get(self, v):
        if self.p[v] == -1:
            return v
        self.p[v] = self.get(self.p[v])
        return self.p[v]

    def unite(self, v, u):
        v = self.get(v)
        u = self.get(u)
        if v == u:
            return
        self.p[v] = u

n, m = map(int, input().split())
real = Dsu(m)
a = list(map(lambda x: int(x) - 1, input().split()))

idxs = [[] for i in range(n)]
for i in range(n):
    idxs[a[i]].append(i)

ans = 0
for i in range(1, n):
    if a[i] != a[i - 1]:
        ans += 1
print(ans)

for _ in range(1, m):
    v, u = map(lambda x: int(x) - 1, input().split())
    v = real.get(v)
    u = real.get(u)
    if len(idxs[v]) > len(idxs[u]):
        v, u = u, v
    real.unite(v, u)
    if v != u:
        for i in idxs[v]:
            if i >= 1 and a[i] == v and a[i - 1] == u:
                ans -= 1
            if i + 1 < n and a[i] == v and a[i + 1] == u:
                ans -= 1
        for i in idxs[v]:
            idxs[u].append(i)
            a[i] = u;
    print(ans) 

import sys
input = sys.stdin.readline
n,m = map(int,input().split())
a = list(map(int,input().split()))
s = [set() for i in range(m+1)]
u = [-1]*(m+1)

for i in range(n):
    s[a[i]].add(i+1)

def find(x):
    if u[x] <= 0:
        return x
    else:
        u[x] = find(u[x])
        return u[x]

count = 0
for i in range(n-1):
    if a[i] != a[i+1]:
        count += 1
ans = [count]

for i in range(1,m):
    x,y = map(int,input().split())
    x = find(x)
    y = find(y)
    if len(s[x]) < len(s[y]):
        x,y = y,x
    for j in s[y]:
        if j-1 in s[x]:
            count -= 1
        if j+1 in s[x]:
            count -= 1
    ans.append(count)
    s[x] |= s[y]
    u[y] = x

for i in ans:
    print(i)

class UnionFindVerSize():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def calc_group_num(self):
        N = len(self._parent)
        ans = 0
        for i in range(N):
            if self.find_root(i) == i:
                ans += 1
        return ans

import sys

input=sys.stdin.readline

n,m=list(map(int,input().split()))
t=list(map(int,input().split()))
t=[t[i]-1 for i in range(n)]
query=[]
for i in range(m-1):
    a,b=list(map(int,input().split()))
    query.append((a-1,b-1))

tower=[[] for i in range(m)]
for i in range(n):
    id=t[i]
    if not tower[id]:
        tower[id].append((i+1,i+1))
    else:
        start,end=tower[id].pop()
        if end+1==i+1:
            tower[id].append((start,i+1))
        else:
            tower[id].append((start,end))
            tower[id].append((i+1,i+1))

#print(tower)
test=[]
for i in range(m):
    for start,end in tower[i]:
        if end!=n:
            test.append((t[start-1],t[end]))

#print(test)
start=[-1]*len(test)
end=[m-1]*len(test)
temp=[[] for i in range(m)]
for i in range(len(test)):
    T=(end[i]+start[i])//2
    temp[T].append(i)


def parabisect():
    uf=UnionFindVerSize(m)
    for i in range(m-1):
        a,b=query[i]
        uf.unite(a,b)
        while temp[i]:
            j=temp[i].pop()
            id1,id2=test[j]
            if uf.is_same_group(id1,id2):
                end[j]=i
            else:
                start[j]=i
            if end[j]-start[j]>1:
                T=(end[j]+start[j])//2
                temp[T].append(j)

for i in range(20):
    parabisect()

res=[0]*m
for i in range(len(test)):
    res[end[i]+1]-=1

for i in range(1,m):
    res[i]+=res[i-1]

for i in range(m):
    print(len(test)+res[i])