#include <bits/stdc++.h>
using namespace std;
struct Def {
  int open = 0, close = 0, pos = 0;
  char c;
  Def(int o, int cl, int p, char ch) {
    open = o;
    close = cl;
    pos = p;
    c = ch;
  }
};
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  if (n % 2 == 1) {
    cout << -1 << endl;
    return 0;
  }
  stack<Def> stk;
  int sol = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == ')') {
      if (stk.empty() == true)
        stk.push(Def(0, 1, i, ')'));
      else if (stk.top().c == '(')
        stk.pop();
      else {
        Def temp = stk.top();
        stk.push(Def(temp.open, temp.close + 1, i, ')'));
      }
    } else {
      if (stk.empty() == true)
        stk.push(Def(1, 0, i, '('));
      else {
        Def temp = stk.top();
        if (temp.open == temp.close) {
          int end = temp.pos;
          int start = 0;
          while (stk.empty() == false) {
            start = stk.top().pos;
            stk.pop();
          }
          sol += end - start + 1;
          stk.push(Def(1, 0, i, '('));
        } else
          stk.push(Def(temp.open + 1, temp.close, i, '('));
      }
    }
  }
  if (stk.empty() == false) {
    Def temp = stk.top();
    if (temp.open == temp.close) {
      int end = temp.pos;
      int start = 0;
      while (stk.empty() == false) {
        start = stk.top().pos;
        stk.pop();
      }
      sol += end - start + 1;
    } else
      sol = -1;
  }
  cout << sol;
  return 0;
}
