#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int lbrackets = 0, rbrackets = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '(')
      lbrackets++;
    else
      rbrackets++;
  }
  if (lbrackets != rbrackets) {
    cout << -1 << endl;
    return 0;
  }
  int ans = 0, left = 0;
  stack<char> stk;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '(') {
      if (stk.empty())
        stk.push(s[i]);
      else {
        if (stk.top() == ')') {
          stk.pop();
          if (stk.empty()) {
            ans += i - left + 1;
          }
        } else {
          stk.push(s[i]);
        }
      }
    } else {
      if (stk.empty()) {
        stk.push(s[i]);
        left = i;
      } else {
        if (stk.top() == ')') {
          stk.push(s[i]);
        } else {
          stk.pop();
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
