#include <bits/stdc++.h>
using namespace std;
const int N = 4e4 + 9;
const long long MOD = 1e9 + 7;
const int INF = 1e9;
int dx[5] = {0, -1, 1, 0, 0};
int dy[5] = {0, 0, 0, -1, 1};
char dc[5] = {'0', 'U', 'D', 'L', 'R'};
int main() {
  int n;
  string s;
  cin >> n;
  cin >> s;
  int d = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(')
      d++;
    else
      d--;
  }
  if (d != 0) {
    cout << -1;
    return 0;
  }
  int j = -1, ans = 0;
  d = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(') {
      if (d == -1) {
        ans += i - j + 1;
        j = -1;
      }
      d++;
    } else {
      d--;
      if (j == -1 && d < 0) j = i;
    }
  }
  cout << ans;
  return 0;
}
