#include <bits/stdc++.h>
using namespace std;
int dp[25][25][5][25][25];
int grid[25][25], row[25][25], col[25][25];
int n, m;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int ans = 0, cnt = 0;
int id = 0;
int main() {
  string s;
  cin >> n;
  scanf("\n");
  getline(cin, s);
  if (n % 2 == 1) {
    cout << -1;
    return 0;
  }
  int sum = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(')
      sum++;
    else
      sum--;
    if (sum == -1) {
      int cac = 0;
      int ok = 0;
      for (int j = i; j < n; j++) {
        if (s[j] == '(')
          cac++;
        else
          cac--;
        if (cac == 0) {
          ans += (j - i + 1);
          i = j;
          break;
        }
      }
      if (cac != 0) {
        cout << -1;
        return 0;
      }
      sum = 0;
    }
  }
  if (sum != 0) {
    cout << -1;
    return 0;
  }
  cout << ans;
}
