#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
stack<char> zhan;
const int maxn = 40005;
int n, i, num1, num2, ans;
string s;
int main() {
  n = read();
  cin >> s;
  num1 = num2 = 0;
  for (i = 0; i < n; i++) {
    if (s[i] == '(')
      num1++;
    else
      num2++;
  }
  if (num1 != num2) {
    cout << -1 << endl;
    return 0;
  } else {
    for (i = 0; i < n; i++) {
      if (zhan.size() == 0) {
        zhan.push(s[i]);
        continue;
      }
      if (s[i] == '(') {
        if (zhan.top() == ')') {
          zhan.pop();
          ans += 2;
        } else
          zhan.push(s[i]);
      } else if (zhan.top() == '(')
        zhan.pop();
      else
        zhan.push(s[i]);
    }
  }
  cout << ans << endl;
  return 0;
}
