#include <bits/stdc++.h>
using namespace std;
const int MAX_CHARS = 256;
long long countDigit(long long n) { return floor(log10(n) + 1); }
long long ipow(long long base, long long exp) {
  long long result = 1;
  for (;;) {
    if (exp & 1) result *= base;
    exp >>= 1;
    if (!exp) break;
    base *= base;
  }
  return result;
}
long long divi(long long n) {
  long long r;
  if (n % 2 == 0) {
    return 2;
  }
  if (n % 3 == 0) {
    return 3;
  } else {
    r = floor(sqrt(n));
    long long d = 5;
    while ((n % d) && (d < n / d)) d += 2;
    if (n % d == 0)
      return d;
    else
      return n;
  }
}
int printmaxSubArraySum(int a[], int size) {
  int max_so_far = INT_MIN, max_ending_here = 0, start = 0, end = 0, s = 0;
  for (int i = 0; i < size; i++) {
    max_ending_here += a[i];
    if (max_so_far < max_ending_here) {
      max_so_far = max_ending_here;
      start = s;
      end = i;
    }
    if (max_ending_here < 0) {
      max_ending_here = 0;
      s = i + 1;
    }
  }
  cout << max_so_far << endl;
  cout << "Starting index " << start << endl << "Ending index " << end << endl;
}
bool isSubsetSum(int arr[], int n, int sum) {
  bool subset[2][sum + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= sum; j++) {
      if (j == 0)
        subset[i % 2][j] = true;
      else if (i == 0)
        subset[i % 2][j] = false;
      else if (arr[i - 1] <= j)
        subset[i % 2][j] =
            subset[(i + 1) % 2][j - arr[i - 1]] || subset[(i + 1) % 2][j];
      else
        subset[i % 2][j] = subset[(i + 1) % 2][j];
    }
  }
  return subset[n % 2][sum];
}
void printCharWithFreq(string str) {
  int n = str.size();
  int freq[26];
  memset(freq, 0, sizeof(freq));
  for (int i = 0; i < n; i++) freq[str[i] - 'a']++;
  for (int i = 0; i < n; i++) {
    if (freq[str[i] - 'a'] != 0) {
      cout << str[i] << freq[str[i] - 'a'] << " ";
      freq[str[i] - 'a'] = 0;
    }
  }
}
string evenlength(string n) {
  string res = n;
  for (int j = n.length() - 1; j >= 0; --j) res += n[j];
  return res;
}
long long mostFrequent(vector<long long> arr, long long n) {
  sort(arr.begin(), arr.end());
  long long max_count = 1, res = arr[0], curr_count = 1;
  for (long long i = 1; i < n; i++) {
    if (arr[i] == arr[i - 1])
      curr_count++;
    else {
      if (curr_count > max_count) {
        max_count = curr_count;
        res = arr[i - 1];
      }
      curr_count = 1;
    }
  }
  if (curr_count > max_count) {
    max_count = curr_count;
    res = arr[n - 1];
  }
  return res;
}
bool ck(int N) {
  if ((sqrt(N) - floor(sqrt(N))) != 0) return false;
  return true;
}
long long gcs(long long N) {
  if (ck(N)) {
    return N;
  }
  long long aboveN = -1, belowN = -1;
  long long n1;
  n1 = N + 1;
  while (true) {
    if (ck(n1)) {
      aboveN = n1;
      break;
    } else
      n1++;
  }
  return aboveN;
}
bool is_prime(long long n) {
  if (n == 1) {
    return false;
  }
  long long i = 2;
  while (i * i <= n) {
    if (n % i == 0) {
      return false;
    }
    i += 1;
  }
  return true;
}
long long leastPrimeFactor(long long n) {
  vector<int> least_prime(n + 1, 0);
  least_prime[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (least_prime[i] == 0) {
      least_prime[i] = i;
      for (int j = 2 * i; j <= n; j += i)
        if (least_prime[j] == 0) least_prime[j] = i;
    }
  }
  return least_prime[n];
}
long long smallestDivisor(long long n) {
  if (n % 2 == 0) return 2;
  for (long long i = 3; i * i <= n; i += 2) {
    if (n % i == 0) return i;
  }
  return n;
}
long long countDistinct(long long arr[], long long n) {
  unordered_set<int> s;
  long long res = 0;
  for (long long i = 0; i < n; i++) {
    if (s.find(arr[i]) == s.end()) {
      s.insert(arr[i]);
      res++;
    }
  }
  return res;
}
void countFreq(long long arr[], long long n) {
  vector<long long> v;
  vector<bool> visited(n, false);
  for (long long i = 0; i < n; i++) {
    if (visited[i] == true) continue;
    long long count = 1;
    for (long long j = i + 1; j < n; j++) {
      if (arr[i] == arr[j]) {
        visited[j] = true;
        count++;
      }
    }
    v.push_back(count);
  }
  long long f = 0;
  for (long long i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
    if (v[i] < 3) {
      f = 1;
    }
  }
  if (f == 1) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
  }
}
long long mcm(long long p[], long long n) {
  long long m[n][n];
  long long j, k, q;
  for (long long i = 1; i < n; i++) {
    m[i][i] = 0;
  }
  for (long long L = 2; L < n; L++) {
    for (long long i = 1; i < n - L + 1; i++) {
      j = i + L - 1;
      m[i][j] = INT_MAX;
      for (k = i; k <= j - 1; k++) {
        q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
        if (q < m[i][j]) m[i][j] = q;
      }
    }
  }
  return m[1][n - 1];
}
long long lis(long long a[], long long n) {
  int lis[n];
  lis[0] = 1;
  for (int i = 1; i < n; i++) {
    lis[i] = 1;
    for (int j = 0; j < i; j++) {
      if (a[i] >= a[j] && lis[i] < lis[j] + 1) {
        lis[i] = lis[j] + 1;
      }
    }
  }
  sort(lis, lis + n);
}
bool isPerfectSquare(long double x) {
  long double sr = sqrt(x);
  return ((sr - floor(sr)) == 0);
}
long long findSubString(string str) {
  int n = str.length();
  int dist_count = 0;
  bool visited[MAX_CHARS] = {false};
  for (int i = 0; i < n; i++) {
    if (visited[str[i]] == false) {
      visited[str[i]] = true;
      dist_count++;
    }
  }
  int start = 0, start_index = -1, min_len = INT_MAX;
  int count = 0;
  int curr_count[MAX_CHARS] = {0};
  for (int j = 0; j < n; j++) {
    curr_count[str[j]]++;
    if (curr_count[str[j]] == 1) count++;
    if (count == dist_count) {
      while (curr_count[str[start]] > 1) {
        if (curr_count[str[start]] > 1) curr_count[str[start]]--;
        start++;
      }
      int len_window = j - start + 1;
      if (min_len > len_window) {
        min_len = len_window;
        start_index = start;
      }
    }
  }
  return str.substr(start_index, min_len).length();
}
int bs(long long arr[], long long l, long long r, long long x) {
  while (l <= r) {
    int m = l + (r - l) / 2;
    if (arr[m] == x) return m;
    if (arr[m] < x)
      l = m + 1;
    else
      r = m - 1;
  }
  return -1;
}
void cutrod(int n, int a[], int s) {
  int i;
  int dp[n + 1];
  for (i = 0; i < n + 1; i++) {
    dp[i] = INT_MIN;
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < s; j++)
      if (a[j] <= i) dp[i] = max(dp[i], dp[i - a[j]] + 1);
  }
  cout << dp[n] << endl;
}
int lcs2(string &X, string &Y) {
  int m = X.length(), n = Y.length();
  int L[2][n + 1];
  bool bi;
  for (int i = 0; i <= m; i++) {
    bi = i & 1;
    for (int j = 0; j <= n; j++) {
      if (i == 0 || j == 0)
        L[bi][j] = 0;
      else if (X[i - 1] == Y[j - 1])
        L[bi][j] = L[1 - bi][j - 1] + 1;
      else
        L[bi][j] = max(L[1 - bi][j], L[bi][j - 1]);
    }
  }
  return L[bi][n];
}
bool ckpal(string s, long long n) {
  for (long long i = 0; i < n; i++) {
    if (s[i] != s[n - i - 1]) {
      return false;
      break;
    }
  }
  return true;
}
bool is_reverse(string s1, string s2, long long m) {
  long long l1 = m;
  long long l2 = m;
  long long index = 0;
  if (index > l1 - 1) return true;
  if (l1 != l2 || s1[index] != s2[l2 - index - 1]) return false;
  ++index;
  return is_reverse(s1, s2, index);
}
bool compare(char arr1[], char arr2[]) {
  for (int i = 0; i < 256; i++)
    if (arr1[i] != arr2[i]) return false;
  return true;
}
bool search(string pat, string txt) {
  int M = pat.size(), N = txt.size();
  char countP[256] = {0}, countTW[256] = {0};
  for (int i = 0; i < M; i++) {
    (countP[pat[i]])++;
    (countTW[txt[i]])++;
  }
  for (int i = M; i < N; i++) {
    if (compare(countP, countTW)) return true;
    (countTW[txt[i]])++;
    countTW[txt[i - M]]--;
  }
  if (compare(countP, countTW)) return true;
  return false;
}
long long sd(long long n) {
  if (n % 2 == 0) return 2;
  for (long long i = 3; i * i <= n; i += 2) {
    if (n % i == 0) return i;
  }
  return n;
}
bool isorted(long long a[], long long n) {
  for (long long i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      return false;
    }
  }
  return true;
}
bool isSubsetSum(long long arr[], long long n, long long sum) {
  bool subset[n + 1][sum + 1];
  for (int i = 0; i <= n; i++) subset[i][0] = true;
  for (int i = 1; i <= sum; i++) subset[0][i] = false;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= sum; j++) {
      if (j < arr[i - 1]) subset[i][j] = subset[i - 1][j];
      if (j >= arr[i - 1])
        subset[i][j] = subset[i - 1][j] || subset[i - 1][j - arr[i - 1]];
    }
  }
  return subset[n][sum];
}
int findMinRec(int arr[], int i, int sumCalculated, int sumTotal) {
  if (i == 0) return abs((sumTotal - sumCalculated) - sumCalculated);
  return min(findMinRec(arr, i - 1, sumCalculated + arr[i - 1], sumTotal),
             findMinRec(arr, i - 1, sumCalculated, sumTotal));
}
int findMin(int arr[], int n) {
  int sumTotal = 0;
  for (int i = 0; i < n; i++) sumTotal += arr[i];
  return findMinRec(arr, n, 0, sumTotal);
}
unsigned long long ceil(unsigned long long n, unsigned long long i) {
  if (n == 0) return 0;
  return (n - 1) / i + 1;
}
long long digisum(long long n) {
  long long s = 0;
  while (n != 0) {
    s += n % 10;
    n /= 10;
  }
  return s;
}
long long mio(vector<long long> v, long long n) {
  long long c = 0;
  for (long long i = 0; i < n; i++) {
    if (v[i] != -1) {
      c++;
    }
  }
  return c;
}
int count(int n) {
  int m = 3;
  int S[2] = {1, 2};
  int table[n + 1];
  memset(table, 0, sizeof(table));
  table[0] = 1;
  for (int i = 0; i < m; i++)
    for (int j = S[i]; j <= n; j++) table[j] += table[j - S[i]];
  return table[n];
}
long long maxHist(long long row[], long long C) {
  stack<long long> result;
  long long top_val;
  long long max_area = 0;
  long long area = 0;
  long long i = 0;
  while (i < C) {
    if (result.empty() || row[result.top()] <= row[i])
      result.push(i++);
    else {
      top_val = row[result.top()];
      result.pop();
      area = top_val * i;
      if (!result.empty()) area = top_val * (i - result.top() - 1);
      max_area = max(area, max_area);
    }
  }
  while (!result.empty()) {
    top_val = row[result.top()];
    result.pop();
    area = top_val * i;
    if (!result.empty()) area = top_val * (i - result.top() - 1);
    max_area = max(area, max_area);
  }
  return max_area;
}
int lenOfLongIncSubArr(int arr[], int n) {
  int max = 1, len = 1;
  for (int i = 1; i < n; i++) {
    if (arr[i] < arr[i - 1])
      len++;
    else {
      if (max < len) max = len;
      len = 1;
    }
  }
  if (max < len) max = len;
  return max;
}
int bitonic(int arr[], int n) {
  int inc[n];
  int dec[n];
  int i, max;
  inc[0] = 1;
  dec[n - 1] = 1;
  for (i = 1; i < n; i++) inc[i] = (arr[i] >= arr[i - 1]) ? inc[i - 1] + 1 : 1;
  for (i = n - 2; i >= 0; i--)
    dec[i] = (arr[i] >= arr[i + 1]) ? dec[i + 1] + 1 : 1;
  int len = 0;
  return lenOfLongIncSubArr(dec, n) + lenOfLongIncSubArr(inc, n) - 1;
}
int findBitonicSubarray(int A[], int n) {
  int I[n + 1];
  I[0] = 1;
  for (int i = 1; i <= n; i++) {
    I[i] = 1;
    if (A[i - 1] < A[i]) I[i] = I[i - 1] + 1;
  }
  int D[n + 1];
  D[n] = 1;
  for (int i = n - 1; i >= 0; i--) {
    D[i] = 1;
    if (A[i] > A[i + 1]) D[i] = D[i + 1] + 1;
  }
  int lbs_len = 1;
  int beg = 0, end = 0;
  for (int i = 0; i <= n; i++) {
    if (lbs_len < I[i] + D[i] - 1) {
      lbs_len = I[i] + D[i] - 1;
      beg = i - I[i] + 1;
      end = i + D[i] - 1;
    }
  }
  printf("The length of longest bitonic sub-array is %d\n", lbs_len);
  printf("The longest bitonic sub-array is [%d, %d]", beg, end);
  return lbs_len;
}
int maxSubArraySum(int a[], int size) {
  int max_so_far = 0, max_ending_here = 0;
  for (int i = 0; i < size; i++) {
    max_ending_here = max_ending_here + a[i];
    if (max_ending_here < 0)
      max_ending_here = 0;
    else if (max_so_far < max_ending_here)
      max_so_far = max_ending_here;
  }
  return max_so_far;
}
long long binarySearch(long long arr[], long long x, long long low,
                       long long high) {
  long long mid;
  while (low < high) {
    mid = (high + low) / 2;
    if (arr[mid] == x) {
      break;
    } else if (arr[mid] > x) {
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  mid = (high + low) / 2;
  if (x >= arr[mid])
    return mid;
  else
    return mid + 1;
}
bool solve() {
  long long n;
  cin >> n;
  long long a[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (n == 1) {
    return true;
  } else {
    long long i = 0;
    while (1) {
      if (a[i] < a[i + 1]) {
        i++;
      } else if (a[i] == a[i + 1] && a[i] != 0 && a[i - 1] != a[i] - 1) {
        a[i]--;
        i++;
        break;
      } else {
        break;
      }
    }
    while (i < n - 1) {
      if (a[i] <= 0) {
        return false;
      }
      if (a[i] <= a[i + 1]) {
        a[i + 1] = a[i] - 1;
        i++;
      } else {
        i++;
      }
    }
    return true;
  }
}
int cpk(long long n) {
  for (long long i = sqrt(n); i >= 2; i--) {
    if (n % (i * i) == 0) {
      return 0;
    }
  }
  return 1;
}
long long slv(long long c) {
  long long s = 0;
  if (c % 4 == 0) {
    s += 6 + 1;
  }
  if (c % 4 == 1) {
    s += 2 + 3;
  }
  if (c % 4 == 2) {
    s += 4 + 9;
  }
  if (c % 4 == 3) {
    s += 8 + 7;
  }
  return s;
}
long long getSum(long long n) {
  long long sum = 0;
  while (n != 0) {
    sum = sum + n % 10;
    n = n / 10;
  }
  return sum;
}
void findPairs(long long arr[], long long n) {
  long long c = 0;
  unordered_map<long long, vector<pair<long long, long long>>> map;
  for (long long i = 0; i < n - 1; i++) {
    for (long long j = i + 1; j < n; j++) {
      long long sum = arr[i] + arr[j];
      if (map.find(sum) != map.end()) {
        for (auto pair : map.find(sum)->second) {
          int m = pair.first, n = pair.second;
          if ((m != i && m != j) && (n != i && n != j)) {
            cout << "Pair First(" << arr[i] << ", " << arr[j]
                 << ")\nPair Second (" << arr[m] << ", " << arr[n] << ")";
            return;
          }
        }
      }
      map[sum].push_back({i, j});
    }
  }
  cout << "No such non-overlapping pairs present";
}
long long countEqualSum(long long arr[], long long n) {
  long long sum[20] = {0};
  long long maxC = 0;
  for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++) {
      sum[arr[i] + arr[j]]++;
    }
  for (int i = 0; i < 20; i++)
    if (sum[i] > maxC) maxC = sum[i];
  return maxC;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    long long n, c = 0, a = 0;
    cin >> n;
    string s;
    cin >> s;
    for (long long i = 0; i < n; i++) {
      if (s[i] == '(') {
        c++;
      } else {
        c--;
        if (c < 0) {
          a += 2;
        }
      }
    }
    if (c == 0) {
      cout << a << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}

### Prompt

Your task is to create a Cpp solution to the following problem:

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 ≤ n ≤ 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.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHARS = 256;
long long countDigit(long long n) { return floor(log10(n) + 1); }
long long ipow(long long base, long long exp) {
  long long result = 1;
  for (;;) {
    if (exp & 1) result *= base;
    exp >>= 1;
    if (!exp) break;
    base *= base;
  }
  return result;
}
long long divi(long long n) {
  long long r;
  if (n % 2 == 0) {
    return 2;
  }
  if (n % 3 == 0) {
    return 3;
  } else {
    r = floor(sqrt(n));
    long long d = 5;
    while ((n % d) && (d < n / d)) d += 2;
    if (n % d == 0)
      return d;
    else
      return n;
  }
}
int printmaxSubArraySum(int a[], int size) {
  int max_so_far = INT_MIN, max_ending_here = 0, start = 0, end = 0, s = 0;
  for (int i = 0; i < size; i++) {
    max_ending_here += a[i];
    if (max_so_far < max_ending_here) {
      max_so_far = max_ending_here;
      start = s;
      end = i;
    }
    if (max_ending_here < 0) {
      max_ending_here = 0;
      s = i + 1;
    }
  }
  cout << max_so_far << endl;
  cout << "Starting index " << start << endl << "Ending index " << end << endl;
}
bool isSubsetSum(int arr[], int n, int sum) {
  bool subset[2][sum + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= sum; j++) {
      if (j == 0)
        subset[i % 2][j] = true;
      else if (i == 0)
        subset[i % 2][j] = false;
      else if (arr[i - 1] <= j)
        subset[i % 2][j] =
            subset[(i + 1) % 2][j - arr[i - 1]] || subset[(i + 1) % 2][j];
      else
        subset[i % 2][j] = subset[(i + 1) % 2][j];
    }
  }
  return subset[n % 2][sum];
}
void printCharWithFreq(string str) {
  int n = str.size();
  int freq[26];
  memset(freq, 0, sizeof(freq));
  for (int i = 0; i < n; i++) freq[str[i] - 'a']++;
  for (int i = 0; i < n; i++) {
    if (freq[str[i] - 'a'] != 0) {
      cout << str[i] << freq[str[i] - 'a'] << " ";
      freq[str[i] - 'a'] = 0;
    }
  }
}
string evenlength(string n) {
  string res = n;
  for (int j = n.length() - 1; j >= 0; --j) res += n[j];
  return res;
}
long long mostFrequent(vector<long long> arr, long long n) {
  sort(arr.begin(), arr.end());
  long long max_count = 1, res = arr[0], curr_count = 1;
  for (long long i = 1; i < n; i++) {
    if (arr[i] == arr[i - 1])
      curr_count++;
    else {
      if (curr_count > max_count) {
        max_count = curr_count;
        res = arr[i - 1];
      }
      curr_count = 1;
    }
  }
  if (curr_count > max_count) {
    max_count = curr_count;
    res = arr[n - 1];
  }
  return res;
}
bool ck(int N) {
  if ((sqrt(N) - floor(sqrt(N))) != 0) return false;
  return true;
}
long long gcs(long long N) {
  if (ck(N)) {
    return N;
  }
  long long aboveN = -1, belowN = -1;
  long long n1;
  n1 = N + 1;
  while (true) {
    if (ck(n1)) {
      aboveN = n1;
      break;
    } else
      n1++;
  }
  return aboveN;
}
bool is_prime(long long n) {
  if (n == 1) {
    return false;
  }
  long long i = 2;
  while (i * i <= n) {
    if (n % i == 0) {
      return false;
    }
    i += 1;
  }
  return true;
}
long long leastPrimeFactor(long long n) {
  vector<int> least_prime(n + 1, 0);
  least_prime[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (least_prime[i] == 0) {
      least_prime[i] = i;
      for (int j = 2 * i; j <= n; j += i)
        if (least_prime[j] == 0) least_prime[j] = i;
    }
  }
  return least_prime[n];
}
long long smallestDivisor(long long n) {
  if (n % 2 == 0) return 2;
  for (long long i = 3; i * i <= n; i += 2) {
    if (n % i == 0) return i;
  }
  return n;
}
long long countDistinct(long long arr[], long long n) {
  unordered_set<int> s;
  long long res = 0;
  for (long long i = 0; i < n; i++) {
    if (s.find(arr[i]) == s.end()) {
      s.insert(arr[i]);
      res++;
    }
  }
  return res;
}
void countFreq(long long arr[], long long n) {
  vector<long long> v;
  vector<bool> visited(n, false);
  for (long long i = 0; i < n; i++) {
    if (visited[i] == true) continue;
    long long count = 1;
    for (long long j = i + 1; j < n; j++) {
      if (arr[i] == arr[j]) {
        visited[j] = true;
        count++;
      }
    }
    v.push_back(count);
  }
  long long f = 0;
  for (long long i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
    if (v[i] < 3) {
      f = 1;
    }
  }
  if (f == 1) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
  }
}
long long mcm(long long p[], long long n) {
  long long m[n][n];
  long long j, k, q;
  for (long long i = 1; i < n; i++) {
    m[i][i] = 0;
  }
  for (long long L = 2; L < n; L++) {
    for (long long i = 1; i < n - L + 1; i++) {
      j = i + L - 1;
      m[i][j] = INT_MAX;
      for (k = i; k <= j - 1; k++) {
        q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
        if (q < m[i][j]) m[i][j] = q;
      }
    }
  }
  return m[1][n - 1];
}
long long lis(long long a[], long long n) {
  int lis[n];
  lis[0] = 1;
  for (int i = 1; i < n; i++) {
    lis[i] = 1;
    for (int j = 0; j < i; j++) {
      if (a[i] >= a[j] && lis[i] < lis[j] + 1) {
        lis[i] = lis[j] + 1;
      }
    }
  }
  sort(lis, lis + n);
}
bool isPerfectSquare(long double x) {
  long double sr = sqrt(x);
  return ((sr - floor(sr)) == 0);
}
long long findSubString(string str) {
  int n = str.length();
  int dist_count = 0;
  bool visited[MAX_CHARS] = {false};
  for (int i = 0; i < n; i++) {
    if (visited[str[i]] == false) {
      visited[str[i]] = true;
      dist_count++;
    }
  }
  int start = 0, start_index = -1, min_len = INT_MAX;
  int count = 0;
  int curr_count[MAX_CHARS] = {0};
  for (int j = 0; j < n; j++) {
    curr_count[str[j]]++;
    if (curr_count[str[j]] == 1) count++;
    if (count == dist_count) {
      while (curr_count[str[start]] > 1) {
        if (curr_count[str[start]] > 1) curr_count[str[start]]--;
        start++;
      }
      int len_window = j - start + 1;
      if (min_len > len_window) {
        min_len = len_window;
        start_index = start;
      }
    }
  }
  return str.substr(start_index, min_len).length();
}
int bs(long long arr[], long long l, long long r, long long x) {
  while (l <= r) {
    int m = l + (r - l) / 2;
    if (arr[m] == x) return m;
    if (arr[m] < x)
      l = m + 1;
    else
      r = m - 1;
  }
  return -1;
}
void cutrod(int n, int a[], int s) {
  int i;
  int dp[n + 1];
  for (i = 0; i < n + 1; i++) {
    dp[i] = INT_MIN;
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < s; j++)
      if (a[j] <= i) dp[i] = max(dp[i], dp[i - a[j]] + 1);
  }
  cout << dp[n] << endl;
}
int lcs2(string &X, string &Y) {
  int m = X.length(), n = Y.length();
  int L[2][n + 1];
  bool bi;
  for (int i = 0; i <= m; i++) {
    bi = i & 1;
    for (int j = 0; j <= n; j++) {
      if (i == 0 || j == 0)
        L[bi][j] = 0;
      else if (X[i - 1] == Y[j - 1])
        L[bi][j] = L[1 - bi][j - 1] + 1;
      else
        L[bi][j] = max(L[1 - bi][j], L[bi][j - 1]);
    }
  }
  return L[bi][n];
}
bool ckpal(string s, long long n) {
  for (long long i = 0; i < n; i++) {
    if (s[i] != s[n - i - 1]) {
      return false;
      break;
    }
  }
  return true;
}
bool is_reverse(string s1, string s2, long long m) {
  long long l1 = m;
  long long l2 = m;
  long long index = 0;
  if (index > l1 - 1) return true;
  if (l1 != l2 || s1[index] != s2[l2 - index - 1]) return false;
  ++index;
  return is_reverse(s1, s2, index);
}
bool compare(char arr1[], char arr2[]) {
  for (int i = 0; i < 256; i++)
    if (arr1[i] != arr2[i]) return false;
  return true;
}
bool search(string pat, string txt) {
  int M = pat.size(), N = txt.size();
  char countP[256] = {0}, countTW[256] = {0};
  for (int i = 0; i < M; i++) {
    (countP[pat[i]])++;
    (countTW[txt[i]])++;
  }
  for (int i = M; i < N; i++) {
    if (compare(countP, countTW)) return true;
    (countTW[txt[i]])++;
    countTW[txt[i - M]]--;
  }
  if (compare(countP, countTW)) return true;
  return false;
}
long long sd(long long n) {
  if (n % 2 == 0) return 2;
  for (long long i = 3; i * i <= n; i += 2) {
    if (n % i == 0) return i;
  }
  return n;
}
bool isorted(long long a[], long long n) {
  for (long long i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      return false;
    }
  }
  return true;
}
bool isSubsetSum(long long arr[], long long n, long long sum) {
  bool subset[n + 1][sum + 1];
  for (int i = 0; i <= n; i++) subset[i][0] = true;
  for (int i = 1; i <= sum; i++) subset[0][i] = false;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= sum; j++) {
      if (j < arr[i - 1]) subset[i][j] = subset[i - 1][j];
      if (j >= arr[i - 1])
        subset[i][j] = subset[i - 1][j] || subset[i - 1][j - arr[i - 1]];
    }
  }
  return subset[n][sum];
}
int findMinRec(int arr[], int i, int sumCalculated, int sumTotal) {
  if (i == 0) return abs((sumTotal - sumCalculated) - sumCalculated);
  return min(findMinRec(arr, i - 1, sumCalculated + arr[i - 1], sumTotal),
             findMinRec(arr, i - 1, sumCalculated, sumTotal));
}
int findMin(int arr[], int n) {
  int sumTotal = 0;
  for (int i = 0; i < n; i++) sumTotal += arr[i];
  return findMinRec(arr, n, 0, sumTotal);
}
unsigned long long ceil(unsigned long long n, unsigned long long i) {
  if (n == 0) return 0;
  return (n - 1) / i + 1;
}
long long digisum(long long n) {
  long long s = 0;
  while (n != 0) {
    s += n % 10;
    n /= 10;
  }
  return s;
}
long long mio(vector<long long> v, long long n) {
  long long c = 0;
  for (long long i = 0; i < n; i++) {
    if (v[i] != -1) {
      c++;
    }
  }
  return c;
}
int count(int n) {
  int m = 3;
  int S[2] = {1, 2};
  int table[n + 1];
  memset(table, 0, sizeof(table));
  table[0] = 1;
  for (int i = 0; i < m; i++)
    for (int j = S[i]; j <= n; j++) table[j] += table[j - S[i]];
  return table[n];
}
long long maxHist(long long row[], long long C) {
  stack<long long> result;
  long long top_val;
  long long max_area = 0;
  long long area = 0;
  long long i = 0;
  while (i < C) {
    if (result.empty() || row[result.top()] <= row[i])
      result.push(i++);
    else {
      top_val = row[result.top()];
      result.pop();
      area = top_val * i;
      if (!result.empty()) area = top_val * (i - result.top() - 1);
      max_area = max(area, max_area);
    }
  }
  while (!result.empty()) {
    top_val = row[result.top()];
    result.pop();
    area = top_val * i;
    if (!result.empty()) area = top_val * (i - result.top() - 1);
    max_area = max(area, max_area);
  }
  return max_area;
}
int lenOfLongIncSubArr(int arr[], int n) {
  int max = 1, len = 1;
  for (int i = 1; i < n; i++) {
    if (arr[i] < arr[i - 1])
      len++;
    else {
      if (max < len) max = len;
      len = 1;
    }
  }
  if (max < len) max = len;
  return max;
}
int bitonic(int arr[], int n) {
  int inc[n];
  int dec[n];
  int i, max;
  inc[0] = 1;
  dec[n - 1] = 1;
  for (i = 1; i < n; i++) inc[i] = (arr[i] >= arr[i - 1]) ? inc[i - 1] + 1 : 1;
  for (i = n - 2; i >= 0; i--)
    dec[i] = (arr[i] >= arr[i + 1]) ? dec[i + 1] + 1 : 1;
  int len = 0;
  return lenOfLongIncSubArr(dec, n) + lenOfLongIncSubArr(inc, n) - 1;
}
int findBitonicSubarray(int A[], int n) {
  int I[n + 1];
  I[0] = 1;
  for (int i = 1; i <= n; i++) {
    I[i] = 1;
    if (A[i - 1] < A[i]) I[i] = I[i - 1] + 1;
  }
  int D[n + 1];
  D[n] = 1;
  for (int i = n - 1; i >= 0; i--) {
    D[i] = 1;
    if (A[i] > A[i + 1]) D[i] = D[i + 1] + 1;
  }
  int lbs_len = 1;
  int beg = 0, end = 0;
  for (int i = 0; i <= n; i++) {
    if (lbs_len < I[i] + D[i] - 1) {
      lbs_len = I[i] + D[i] - 1;
      beg = i - I[i] + 1;
      end = i + D[i] - 1;
    }
  }
  printf("The length of longest bitonic sub-array is %d\n", lbs_len);
  printf("The longest bitonic sub-array is [%d, %d]", beg, end);
  return lbs_len;
}
int maxSubArraySum(int a[], int size) {
  int max_so_far = 0, max_ending_here = 0;
  for (int i = 0; i < size; i++) {
    max_ending_here = max_ending_here + a[i];
    if (max_ending_here < 0)
      max_ending_here = 0;
    else if (max_so_far < max_ending_here)
      max_so_far = max_ending_here;
  }
  return max_so_far;
}
long long binarySearch(long long arr[], long long x, long long low,
                       long long high) {
  long long mid;
  while (low < high) {
    mid = (high + low) / 2;
    if (arr[mid] == x) {
      break;
    } else if (arr[mid] > x) {
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  mid = (high + low) / 2;
  if (x >= arr[mid])
    return mid;
  else
    return mid + 1;
}
bool solve() {
  long long n;
  cin >> n;
  long long a[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (n == 1) {
    return true;
  } else {
    long long i = 0;
    while (1) {
      if (a[i] < a[i + 1]) {
        i++;
      } else if (a[i] == a[i + 1] && a[i] != 0 && a[i - 1] != a[i] - 1) {
        a[i]--;
        i++;
        break;
      } else {
        break;
      }
    }
    while (i < n - 1) {
      if (a[i] <= 0) {
        return false;
      }
      if (a[i] <= a[i + 1]) {
        a[i + 1] = a[i] - 1;
        i++;
      } else {
        i++;
      }
    }
    return true;
  }
}
int cpk(long long n) {
  for (long long i = sqrt(n); i >= 2; i--) {
    if (n % (i * i) == 0) {
      return 0;
    }
  }
  return 1;
}
long long slv(long long c) {
  long long s = 0;
  if (c % 4 == 0) {
    s += 6 + 1;
  }
  if (c % 4 == 1) {
    s += 2 + 3;
  }
  if (c % 4 == 2) {
    s += 4 + 9;
  }
  if (c % 4 == 3) {
    s += 8 + 7;
  }
  return s;
}
long long getSum(long long n) {
  long long sum = 0;
  while (n != 0) {
    sum = sum + n % 10;
    n = n / 10;
  }
  return sum;
}
void findPairs(long long arr[], long long n) {
  long long c = 0;
  unordered_map<long long, vector<pair<long long, long long>>> map;
  for (long long i = 0; i < n - 1; i++) {
    for (long long j = i + 1; j < n; j++) {
      long long sum = arr[i] + arr[j];
      if (map.find(sum) != map.end()) {
        for (auto pair : map.find(sum)->second) {
          int m = pair.first, n = pair.second;
          if ((m != i && m != j) && (n != i && n != j)) {
            cout << "Pair First(" << arr[i] << ", " << arr[j]
                 << ")\nPair Second (" << arr[m] << ", " << arr[n] << ")";
            return;
          }
        }
      }
      map[sum].push_back({i, j});
    }
  }
  cout << "No such non-overlapping pairs present";
}
long long countEqualSum(long long arr[], long long n) {
  long long sum[20] = {0};
  long long maxC = 0;
  for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++) {
      sum[arr[i] + arr[j]]++;
    }
  for (int i = 0; i < 20; i++)
    if (sum[i] > maxC) maxC = sum[i];
  return maxC;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    long long n, c = 0, a = 0;
    cin >> n;
    string s;
    cin >> s;
    for (long long i = 0; i < n; i++) {
      if (s[i] == '(') {
        c++;
      } else {
        c--;
        if (c < 0) {
          a += 2;
        }
      }
    }
    if (c == 0) {
      cout << a << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}

```