#include <bits/stdc++.h>
using namespace std;
int main() {
  int l;
  cin >> l;
  string s;
  cin >> s;
  int i;
  int opening = 0, closing = 0;
  for (i = 0; i < l; i++) {
    if (s[i] == '(') {
      opening++;
    } else {
      closing++;
    }
  }
  if (opening != closing) {
    cout << -1 << endl;
  } else {
    int openCount = 0;
    int closeCount = 0;
    int ans = 0;
    for (i = 0; i < l; i++) {
      if (s[i] == ')') {
        if (openCount)
          openCount--;
        else
          closeCount++;
      } else {
        if (closeCount) {
          closeCount--;
          ans += 2;
        } else {
          openCount++;
        }
      }
    }
    cout << ans << endl;
  }
}
