#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  map<char, int> mp;
  for (int i = 0; i < n; ++i) {
    mp[s[i]]++;
  }
  if (n & 1 || mp['('] != mp[')']) {
    cout << -1 << endl;
    return 0;
  }
  int cnt = 0;
  int ans = 0;
  bool flag = 0;
  int len = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '(') {
      cnt++;
    } else if (s[i] == ')') {
      cnt--;
    }
    if (cnt < 0) {
      flag = 1;
      len++;
    } else if (cnt == 0 && flag) {
      ans += len + 1;
      len = 0;
      flag = 0;
    }
  }
  cout << ans << endl;
}
