package finalset;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * You are to write a program that graphs polynomial functions over a small two-dimensional
 * cartesian grid.
 * 
 * The polynomial function is described without spaces and is composed by the addition
 * or subtraction of terms of the form
 * 
 * All exponents and coefficients
 * are restricted to be single digits in the range from 1 through 9 (i.e., the digit 0
 * will not occur in the input).
 * 
 * 
 *    
 * where each capital letter represents  
 * 
 * 
 * 
 * @author George
 *
 */

class Term {
	int coeff;
	int power;
	
	Term (int coeff, int power) {
		this.coeff = coeff;
		this.power = power;
	}
	
	int eval(int x) {
		if (x == 0) {
			// zero if we have a non-zero power, otherwise return coeff
			if (power != 0) return 0;
			return coeff;			
		}
		
		int val = 1;
		for (int i = 1; i <= power; i++) {
			val = val * x;
		}
		
		return val * coeff;
	}
}

public class Problem7 {

	// return string of function graphed.
	public static int process (String function, int x) {
		ArrayList al = new ArrayList();
		
		// parse function
		int i = 0;
		while (i < function.length()) {
			int coeff = 1;
			if (function.charAt(i) == '-') {
				coeff = -1;
				i++;
			} else if (function.charAt(i) == '+') {
				i++;
			}
			
			if (function.charAt(i) == 'x') {
				// coeff = 1; UNCHANGED
				i++;
			} else {
				coeff = coeff * (function.charAt(i++)-'0');
				
				// if we get here, perhaps we are a constant and DONE or x then advance)
				if (i == function.length()) {
					al.add(new Term(coeff,0));
					break;
				} else {
					// we must be at an x, so advance
					i++;
				}
			}
			
			// if we are pointed to a '^' then we are exponent. If end, then we are
			// on the last term
			if (i == function.length()) {
				al.add (new Term (coeff, 1));
				break;
			}
			
			if (function.charAt(i) == '^') {
				i++;  // skip ^
				int p = function.charAt(i++)-'0';
				al.add (new Term (coeff, p));
			} else {
				al.add (new Term (coeff, 1));   // no exponent!
				// i++;  // ADVANCE!  MOVES TOO FAST!
			}
			
			// continue!
			
		}
		
		int sum = 0;
		for (i = 0; i < al.size(); i++) {
			Term t = (Term) al.get(i);
			sum += t.eval(x);
		}
		
		return sum;
	}
	
	public static void main (String args[]) {
		Scanner sc = new Scanner(System.in);
		String function = sc.nextLine();
		int x = Integer.valueOf(sc.nextLine());
		
		// process input
		System.out.println (process(function, x));
	}
}
