import java.io.*;
import java.util.*;

public class solution1 {
	static String pstring;
	static char[] pchars;
	
	static int findTokenEnd(int start) {
		while(start < pchars.length && !Character.isWhitespace(pchars[start]) && pchars[start] != ')') {
			start++;
		}
		return start;
	}
	
	static int findNextTokenStart(int start) {
		while(start < pchars.length && Character.isWhitespace(pchars[start])) {
			start++;
		}
		return start;
	}
	
	static int findClosingParantheses(int start) {
		int count = 1;
		for (; count > 0 && start < pchars.length; start++) {
			if (pchars[start] == '(') {
				count++;
			} else if (pchars[start] == ')') {
				count--;
				if (count == 0) {
					break;
				}
			}
		}
		if (count > 0) {
			System.out.println("Input has unmatched parantheses.");
		}
		return start;
	}
	
	static void findTotalTimes(boolean odd_length, int mult, long times[], long totalTimes[]) {
		if (odd_length) {
			totalTimes[0] = (mult/2)*(times[0]+times[1]) + (mult%2)*(times[0]);
			totalTimes[1] = (mult/2)*(times[0]+times[1]) + (mult%2)*(times[1]);
		} else {
			totalTimes[0] = times[0] * mult;
			totalTimes[1] = times[1] * mult;
		}
		return;
	}
	
	static int doBinarySearch(long left, boolean odd_length, int max_mult, long times[]) {
		long[] recursiveTimes = {0,0};
		int lower = 0;
		int upper = max_mult;
		while (lower < upper - 1) {
			//System.out.println(lower + "\t" + upper);
			int med = (int)(((long)lower + (long)upper)/2);
			findTotalTimes(odd_length, med, times, recursiveTimes);
			if (recursiveTimes[0] < left) {
				lower = med;
			} else {
				upper = med;
			}
		}
		
		return lower;
	}
	
	static boolean computeTimeSpent(int start, int end, int mult, long left, boolean startState, long[] times) {
	    //System.out.println("A:\t" + start + "\t" + end + "\t" + mult + "\t" + left + "\t" + startState);
		// Running counts
		times[0] = 0;
		times[1] = 0;
		boolean state = startState;
		int current = start;
		
		while (current < end) {
			//System.out.println(current);
			if (Character.isDigit(pchars[current])) {
				int tokenEnd = findTokenEnd(current);
				long time = Long.parseLong(pstring.substring(current, tokenEnd));
				if (state) {
					times[0] += time;
					if (times[0] >= left) {
						times[0] = left;
					    break;
					}
				} else {
					times[1] += time;
				}
				state = !state;
				current = findNextTokenStart(tokenEnd);
			} else if (pchars[current] == '(') {
				current++;
				int closing = findClosingParantheses(current);
				long recursiveTimes[] = {0, 0};
				boolean resulting_state;
				if (pchars[closing+1] == '*') {
					int mult_start = closing+2;
					int mult_end = findTokenEnd(mult_start);
					int multiplier = Integer.parseInt(pstring.substring(mult_start, mult_end));
					resulting_state = computeTimeSpent(current, closing, multiplier, left-times[0], state, recursiveTimes);
				} else {
					resulting_state = computeTimeSpent(current, closing, 1, left-times[0], state, recursiveTimes);
				}
				
			    //System.out.println("B:\t" + times[0] + "\t" + times[1] + "\t" + recursiveTimes[0] + "\t" + recursiveTimes[1] + "\t" + resulting_state);
				
				times[1] += recursiveTimes[1];
				times[0] += recursiveTimes[0];
				if (times[0] == left) {
					break;
				}
				state = resulting_state;
				current = findNextTokenStart(findTokenEnd(closing+1));
			} else {
				current = end;
				System.out.println("incorrectly formatted data");
			}
		}
		
		boolean odd_length = (state != startState);
		
		if (times[0] == left) {
			// Not care
			return state;
		} else {
			if (mult > 1) {
				long[] recursiveTimes = {0,0};
				findTotalTimes(odd_length, mult, times, recursiveTimes);
				if (recursiveTimes[0] < left) {
					times[0] = recursiveTimes[0];
					times[1] = recursiveTimes[1];
					return startState ^ (odd_length && mult%2 == 1);
				} else {
					int completed_reps = doBinarySearch(left, odd_length, mult, times);
					long[] completedTimes = {0,0};
					findTotalTimes(odd_length, completed_reps, times, completedTimes);
					boolean intermediate_state = startState ^ (odd_length && completed_reps%2 == 1);
					state = computeTimeSpent(start, end, 1, left-completedTimes[0], intermediate_state, recursiveTimes);
					times[0] = completedTimes[0] + recursiveTimes[0];
					times[1] = completedTimes[1] + recursiveTimes[1];
					// Not care
					return state;
				}
			} else {
				// Not care
				return state;
			}
		}
	}
	
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			long limit = Long.parseLong(in.readLine().trim());
			if (limit == 0) {
				break;
			}
			
			pstring = in.readLine().trim();
			pchars = pstring.toCharArray();
			
			long times[] = {0,0};
			boolean end_state = computeTimeSpent(0, pchars.length, 1, limit, true, times);
		    //System.out.println("C:\t" + times[0] + "\t" + times[1] + "\t" + end_state);

			boolean odd_length = !end_state;
			if (times[0] < limit) {
				int completed_reps = doBinarySearch(limit, odd_length, 2000000001, times);
				long[] completedTimes = {0,0};
				findTotalTimes(odd_length, completed_reps, times, completedTimes);
				boolean intermediate_state = !(odd_length && completed_reps%2 == 1);
				long[] recursiveTimes = {0,0};
				computeTimeSpent(0, pchars.length, 1, limit-completedTimes[0], intermediate_state, recursiveTimes);
				System.out.println(completedTimes[0] + recursiveTimes[0] + completedTimes[1] + recursiveTimes[1]);				
			} else {
				System.out.println(times[0] + times[1]);
			}
		}
	}
}
