#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
const int MAX_LENGTH = 100000; // Maximum input length
// Compute prefix balance array: balance[i] = net open brackets up to i
vector<int> computePrefixBalance(const string& s) {
int n = s.length();
vector<int> balance(n);
int currBalance = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == '(') {
currBalance += 1;
} else {
currBalance -= 1;
}
balance[i] = currBalance;
}
return balance;
}
// Check if the string becomes valid after flipping bracket at position idx
bool isValidAfterFlip(const string& s, int idx, const vector<int>& prefixBalance, int n) {
// Flipping '(' -> ')' decreases balance by 2
// Flipping ')' -> '(' increases balance by 2
int delta = 0;
if (s[idx] == '(') {
delta = -2;
} else {
delta = 2;
}
int currBalance = 0;
int minBalance = 0;
// Prefix before idx
if (idx > 0) {
currBalance = prefixBalance[idx - 1];
if (currBalance < minBalance) {
minBalance = currBalance;
}
}
// Apply flip at idx
currBalance += delta;
if (currBalance < minBalance) {
minBalance = currBalance;
}
// Process suffix (from idx+1 to end)
if (idx < n - 1) {
int suffixBalance = prefixBalance[n - 1] - prefixBalance[idx];
currBalance += suffixBalance;
if (currBalance < minBalance) {
minBalance = currBalance;
}
}
// Valid if final balance = 0 and never negative
if (currBalance == 0 && minBalance >= 0) {
return true;
} else {
return false;
}
}
// Validate input
bool isValidInput(const string& s) {
int n = s.length();
if (n < 1 || n > MAX_LENGTH) {
return false;
}
if (n % 2 != 0) {
return false; // Odd length can't form valid parentheses
}
for (char c : s) {
if (c != '(' && c != ')') {
return false;
}
}
return true;
}
// Count valid sequences obtainable by flipping exactly one bracket
int countValidFlips(const string& s) {
if (!isValidInput(s)) {
return 0;
}
int n = s.length();
vector<int> prefixBalance = computePrefixBalance(s);
// For a single flip to fix the sequence, final balance must be ±2
int finalBalance = prefixBalance[n - 1];
if (abs(finalBalance) != 2) {
return 0;
}
int validCount = 0;
for (int i = 0; i < n; ++i) {
if (isValidAfterFlip(s, i, prefixBalance, n)) {
validCount += 1;
}
}
return validCount;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
cout << countValidFlips(s) << '\n';
return 0;
}