#include <bits/stdc++.h>
using namespace std;
vector<map<int, int>> adj;
vector<vector<int>> ver;
vector<bool> storage;
void Solve() {
  int n;
  cin >> n;
  vector<int> vec(n);
  cin.get();
  for (int i = 0; i < n; ++i) {
    char ch = cin.get();
    if (ch == '(')
      ++vec[i];
    else
      --vec[i];
    if (i > 0) vec[i] += vec[i - 1];
  }
  int ans = 0;
  int l = -1;
  for (int i = 0; i < n; ++i) {
    if (l == -1) {
      if (vec[i] < 0) l = i;
    } else if (vec[i] >= 0) {
      ans += i - l + 1;
      l = -1;
    }
  }
  if (vec[vec.size() - 1] != 0)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  Solve();
}
