#include <bits/stdc++.h>
using namespace std;
int n;
char s[1000010];
int main() {
  cin >> n;
  cin >> s + 1;
  int sc = 0;
  for (register int i = 1; i <= n; i++) {
    if (s[i] == '(')
      sc++;
    else
      sc--;
  }
  if (sc) {
    puts("-1");
    return 0;
  }
  int last = 1, orz = 0, ans = 0, flag = 0;
  for (register int i = 1; i <= n; i++) {
    if (s[i] == ')')
      orz--;
    else
      orz++;
    if (orz) {
      if (last == i && s[i] == ')') {
        flag = 1;
      }
    } else {
      if (flag) ans += i - last + 1, flag = 0;
      last = i + 1;
    }
  }
  cout << ans << endl;
  return 0;
}
