#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  cin >> n;
  string s;
  cin >> s;
  long long int i, j, k;
  long long int cnt = 0;
  i = n - 1;
  stack<char> a;
  while (i >= 0) {
    if (s[i] == ')') {
      a.push(')');
      i--;
      while (!a.empty() && i >= 0) {
        if (s[i] == '(')
          a.pop();
        else if (s[i] == ')')
          a.push(')');
        i--;
      }
    } else {
      a.push('(');
      i--;
      while (!a.empty() && i >= 0) {
        if (s[i] == '(')
          a.push('(');
        else if (i >= 1 && s[i] == ')' && s[i - 1] == '(') {
          i--;
          cnt += 2;
        } else if (s[i] == ')') {
          a.pop();
          cnt += 2;
        }
        i--;
      }
    }
  }
  if (!a.empty() && cnt == 0)
    cout << "-1\n";
  else if (a.empty() && cnt == 0)
    cout << "0\n";
  else if (a.empty() && cnt > 0)
    cout << cnt << "\n";
  else
    cout << "-1\n";
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
}
