#include <bits/stdc++.h>
using namespace std;
int n;
string s;
vector<int> op;
pair<int, int> query(int l, int r) {
  if (l > r) return {0, 0};
  int ope = 0;
  ope = op[r];
  if (l > 0) ope -= op[l - 1];
  int cl = r - l + 1 - ope;
  return {ope, cl};
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  cin >> s;
  op.resize(n);
  if (s[0] == '(') {
    op[0] = 1;
  }
  for (int i = 1; i < n; i++) {
    op[i] = op[i - 1];
    if (s[i] == '(') op[i]++;
  }
  pair<int, int> q = query(0, n - 1);
  if (q.first != q.second) {
    cout << -1 << endl;
    return 0;
  }
  stack<pair<char, int>> pilha;
  int k = n;
  for (int i = 0; i < n; i++) {
    if (!pilha.empty() && pilha.top().first == '(') {
      if (s[i] == ')') {
        pilha.pop();
        pair<int, int> q1;
        if (pilha.empty())
          q1 = {0, 0};
        else
          q1 = query(0, pilha.top().second);
        pair<int, int> q2 = query(i + 1, n - 1);
        if (q1.first == q1.second && q2.first == q2.second) {
          k -= 2;
        } else if (q1.first == q2.second && q1.second == 0 && q2.first == 0) {
          k -= 2;
        } else if (q1.first - q1.second == q2.second - q2.first &&
                   q1.first - q1.second >= 0) {
          k -= 2;
        }
      } else
        pilha.push({s[i], i});
    } else
      pilha.push({s[i], i});
  }
  cout << k << endl;
  return 0;
}
