#include <bits/stdc++.h>
using namespace std;
char s[1000010];
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  cin >> (s + 1);
  int x = 0, y = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '(')
      x++;
    else
      y++;
  }
  if (x != y) {
    cout << -1 << endl;
    return 0;
  }
  int ans = 0;
  x = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '(')
      x++;
    else
      x--;
    if (x < 0) {
      int y = -1, id = 0;
      for (int j = i + 1; j <= n; j++) {
        if (s[j] == '(') {
          y++;
          x++;
        } else {
          y--;
          x--;
        }
        if (y >= 0) {
          id = j;
          break;
        }
      }
      ans += id - i + 1;
      i = id;
    }
  }
  cout << ans << endl;
  return 0;
}
