// Arup Guha
// 10/11/2014
// Solution to 2011 South East Regional Problem: Burnout

// Note: This solution is strange, because almost all of the work is done it the
//       constructor of the term class. The reason it turned out that way is that
//       the determining attributes of a term turn out to be 95% of the problem.

import java.util.*;

public class g {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		long ms = Long.parseLong(stdin.nextLine());

		while (ms != 0) {

			// Awful hack to take care of cases where the original sequence needs to be repeated.
			// Just reusing my multiply code.
			term.str = stdin.nextLine().trim();
			term.str = "(" + term.str + ")*2000000000";

			// Solve it and go to the next case.
			System.out.println(solve(ms));
			ms = Long.parseLong(stdin.nextLine());
		}
	}

	// Recursive function that does it all...almost!
	public static long solve(long ms) {
		term all = new term(0, term.str.length()-1, ms, true);
		return all.result;
	}
}

class term {

	// Stores our input string (with my extra multiply)
	public static String str;

	// Critical attributes of the term.
	public long totalTime;
	public long oddTime;
	public long evenTime;
	public long numTerms;

	// Stores the final result of the problem.
	public long result;

	// How must time we have left for this term.
	public long timeLeft;

	// Actually part of our term object.
	public int startI;
	public int endI;
	public boolean product;
	public boolean startState;
	public long multiplier;

	public term(int s, int e, long myTimeOn, boolean curState) {

		// We know these.
		startI = s;
		endI = e;
		timeLeft = myTimeOn;
		startState = curState;
		product = getIfProduct(s,e);

		// Denotes that the light hasn't burned out yet.
		result = -1;

		// Handle products.
		if (product) {

			// Determine our two key components.
			int newEndI = setMultiplier(e);
			term base = new term(startI+1, newEndI, myTimeOn, startState);

			// Case where light burned out before we finished this sequence.
			if (base.result != -1) {
				result = base.result;
				return;
			}
			else {

				// To avoid overflow due to my initially huge wrapper multiplier.
				if (1000000000000000000L/base.totalTime < multiplier)
					multiplier = 1000000000000000000L/base.totalTime;

				// Easier case.
				if (base.numTerms%2 == 0) {

					// Go ahead and make all of these calculations.
					totalTime = multiplier*base.totalTime;
					oddTime = multiplier*base.oddTime;
					evenTime = multiplier*base.evenTime;
					numTerms = multiplier*base.numTerms;
					long timeOnBase = startState ? base.oddTime : base.evenTime;
					long timeOn = startState ? oddTime : evenTime;

					// We finish in this segment, so set result and return.
					if (timeOn >= timeLeft) {
						long full = (timeLeft-1)/timeOnBase;
						long lastIter = timeLeft - full*timeOnBase;
						result = base.totalTime*full + willLast(base, lastIter, base.startState);
						return;
					}
				}

				// Odd number of terms.
				else {

					totalTime = multiplier*base.totalTime;
					numTerms = multiplier*base.numTerms;

					// Have to segment this into two calculations.
					if (multiplier%2 == 0) {
						oddTime = totalTime/2;
						evenTime = totalTime/2;
					}

					// Last segment "sticks" out.
					else {
						oddTime = (multiplier-1)*base.totalTime/2 + base.oddTime;
						evenTime = (multiplier-1)*base.totalTime/2 + base.evenTime;
					}

					long timeOnBase = startState ? base.oddTime : base.evenTime;
					long timeOn = startState ? oddTime : evenTime;

					// We finish in this segment, so set the result.
					if (timeOn >= timeLeft) {

						// Determine number of cycles.
						long numCycles = (timeLeft-1)/base.totalTime;
						long lastIter = timeLeft - numCycles*base.totalTime;

						// Goes through the last "odd cycle"
						if (lastIter > timeOnBase) {
							lastIter -= timeOnBase;
							result = numCycles*2*base.totalTime + base.totalTime + willLast(base, lastIter, !base.startState);
							return;
						}

						// Finishes on the last "odd cycle"
						else {
							result = numCycles*2*base.totalTime + willLast(base, lastIter, base.startState);
							return;
						}
					}
				} // end else for odd terms in base sequence.
			} // end else for when result isn't set in one iteration of base sequence.
		} // end product case.

		// Summation of terms.
		else {

			// Base case of one number.
			if (isSimpleTerm()) {
				totalTime = Long.parseLong(str.substring(startI,endI+1));
				oddTime = totalTime;
				evenTime = 0;
				numTerms = 1;
				if (startState && totalTime >= timeLeft)
					result = timeLeft;
				return;
			}

			// For initial term in sum sequence.
			int curIndex = startI;
			long curTimeLeft = timeLeft;
			boolean state = startState;

			// Go through all terms.
			while (curIndex <= endI) {

				// Get the next term.
				int nextIndex = getEndIndex(curIndex);
				term thisTerm = new term(curIndex, nextIndex, curTimeLeft, state);

				// Light burns out; return.
				if (thisTerm.result != -1) {
					result = totalTime + thisTerm.result;
					return;
				}

				// Update current information adjusting for this term.
				curIndex = nextIndex+2;
				curTimeLeft -= (state ? thisTerm.oddTime : thisTerm.evenTime);
				if (thisTerm.numTerms%2 == 1) state = !state;

				// Update running counts.
				oddTime +=  (numTerms%2 == 0 ? thisTerm.oddTime : thisTerm.evenTime);
				evenTime += (numTerms%2 == 0 ? thisTerm.evenTime: thisTerm.oddTime);
				totalTime += thisTerm.totalTime;
				numTerms += thisTerm.numTerms;
			}
		} // end summation case for parsing.
	}

	// Returns true iff str[s..e] is a single product.
	public boolean getIfProduct(int s, int e) {
		if (str.charAt(s) != '(') return false;
		int matchingE = getEndIndex(s);
		return matchingE == e;
	}

	// Returns true if the current term is a simple one.
	public boolean isSimpleTerm() {
		if (str.charAt(startI) == '(') return false;
		return getEndIndex(startI) == endI;
	}

	// Returns the ending index of the term starting at curIndex.
	public int getEndIndex(int curIndex) {

		// If it starts with a product, find a matching parenthesis.
		if (str.charAt(curIndex) == '(') {
			int parCnt = 1;
			curIndex++;
			while (parCnt > 0) {
				if (str.charAt(curIndex) == '(') parCnt++;
				else if (str.charAt(curIndex) == ')') parCnt--;
				curIndex++;
			}

			//for times sign.
			curIndex++;
		}

		// Iterates through last number.
		while (curIndex < str.length() && str.charAt(curIndex) >= '0' && str.charAt(curIndex) <= '9') curIndex++;
		return curIndex-1;
	}

	// Can only be called if it will burn out on the base pattern of this multiplicative object.
	// Returns when the light will burn out.
	public long willLast(term base, long timeToBurn, boolean curState) {
		term copy = new term(base.startI, base.endI, timeToBurn, curState);
		return copy.result;
	}

	// Sets the multiplier for this term.
	// Returns the ending index of the sequence INSIDE the multiplier.
	public int setMultiplier(int endIndex) {
		int startIndex = endIndex;
		while (str.charAt(startIndex) != '*') startIndex--;
		startIndex++;
		multiplier = Long.parseLong(str.substring(startIndex, endIndex+1));
		return startIndex-3;
	}
}