A bracketed sequence is called correct (regular) if by inserting "+" and "1" you can get a well-formed mathematical expression from it. For example, sequences "(())()", "()" and "(()(()))" are correct, while ")(", "(()" and "(()))(" are not.

The teacher gave Dmitry's class a very strange task — she asked every student to come up with a sequence of arbitrary length, consisting only of opening and closing brackets. After that all the students took turns naming the sequences they had invented. When Dima's turn came, he suddenly realized that all his classmates got the correct bracketed sequence, and whether he got the correct bracketed sequence, he did not know.

Dima suspects now that he simply missed the word "correct" in the task statement, so now he wants to save the situation by modifying his sequence slightly. More precisely, he can the arbitrary number of times (possibly zero) perform the reorder operation.

The reorder operation consists of choosing an arbitrary consecutive subsegment (substring) of the sequence and then reordering all the characters in it in an arbitrary way. Such operation takes $l$ nanoseconds, where $l$ is the length of the subsegment being reordered. It's easy to see that reorder operation doesn't change the number of opening and closing brackets. For example for "))((" he can choose the substring ")(" and do reorder ")()(" (this operation will take $2$ nanoseconds).

Since Dima will soon have to answer, he wants to make his sequence correct as fast as possible. Help him to do this, or determine that it's impossible.


-----Input-----

The first line contains a single integer $n$ ($1 \le n \le 10^6$) — the length of Dima's sequence.

The second line contains string of length $n$, consisting of characters "(" and ")" only.


-----Output-----

Print a single integer — the minimum number of nanoseconds to make the sequence correct or "-1" if it is impossible to do so.


-----Examples-----
Input
8
))((())(

Output
6

Input
3
(()

Output
-1



-----Note-----

In the first example we can firstly reorder the segment from first to the fourth character, replacing it with "()()", the whole sequence will be "()()())(". And then reorder the segment from the seventh to eighth character, replacing it with "()". In the end the sequence will be "()()()()", while the total time spent is $4 + 2 = 6$ nanoseconds.

Difficulty: competition

Starter code:

Solutions:
__, s = input(), input()
a, n = 0, 0
pt = {'(': 1, ')': -1}
for c in s:
  da = pt.get(c, 0)
  if a < 0 or a + da < 0:
    n += 1
  a += da
if a != 0:
  print(-1)
else:
  print(n)

n = int(input())
s = input()

if n % 2 == 1 or s.count('(') != s.count(')'):
    print(-1)
    return

a = 0
nl = -1
nr = -1
ans = 0
for i in range(n):
    if s[i] == '(':
        a += 1
    else:
        a -= 1
    if a < 0:
        if nl <= -1:
            nl, nr = i, i + 1
        else:
            nr = i + 1
    else:
        if nl != -1:
            ans += nr - nl + 1
            nl, nr = -1, -1
print(ans)

import sys
input = sys.stdin.readline

n=int(input())
S=input().strip()

A=0
B=0
for s in S:
    if s=="(":
        A+=1
    else:
        B+=1

if A!=B:
    print(-1)
    return

NOW=[0]
for i in range(n):
    if S[i]==")":
        NOW.append((NOW[-1]-1))

    else:
        NOW.append((NOW[-1]+1))

score=0
ANS=0
for i in range(n+1):
    if NOW[i]<0:
        score+=1
    if NOW[i-1]<0 and NOW[i]==0:
        ANS+=score+1
        score=0
print(ANS)
        
        


import sys
input = sys.stdin.readline


n = int(input())
s = input()

ans = [0] * (n + 1)
for i in range(n):
    if s[i] == "(":
        ans[i + 1] += ans[i] + 1 
    else:
        ans[i + 1] += ans[i] - 1 

if ans[-1] != 0:
    print(-1)
    return

res = 0
flag = False
for i in range(n + 1):
    if ans[i] == -1 and not flag:
        ind = i
        flag = True
    elif ans[i] == 0 and flag:
        res += i - ind + 1
        flag = False
print(res)

def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    S = input().rstrip('\n')

    T = [0] * N
    for i, s in enumerate(S):
        if s == '(':
            T[i] = 1
        else:
            T[i] = -1

    if sum(T) != 0:
        print(-1)
        return

    for i in range(1, N):
        T[i] += T[i-1]
    ans = 0
    cnt = 0
    for t in T:
        if t < 0:
            cnt += 1
        else:
            if cnt:
                ans += cnt+1
            cnt = 0
    print(ans)



def __starting_point():
    main()

__starting_point()