import java.util.*;
import java.lang.*;
import java.io.*;
import java.awt.Point;

// SHIVAM GUPTA :
//NSIT
//decoder_1671

// STOP NOT TILL IT IS DONE OR U DIE .

// U KNOW THAT IF THIS DAY WILL BE URS THEN NO ONE CAN DEFEAT U HERE................

// ASCII = 48 + i ;// 2^28 = 268,435,456  > 2* 10^8 // log 10 base 2 = 3.3219

// odd:: (x^2+1)/2 , (x^2-1)/2 ; x>=3// even:: (x^2/4)+1 ,(x^2/4)-1  x >=4

// FOR ANY ODD NO N : N,N-1,N-2
//ALL ARE PAIRWISE COPRIME
//THEIR COMBINED LCM IS PRODUCT OF ALL THESE NOS

// two consecutive odds are always coprime to each other
// two consecutive even have always gcd  = 2 ;

public class Main
{
     // static int[] arr = new int[100002] ; // static int[] dp = new int[100002] ;

     static PrintWriter out;

	static class FastReader{
		BufferedReader br;
		StringTokenizer st;
		public FastReader(){
			br=new BufferedReader(new InputStreamReader(System.in));
			out=new PrintWriter(System.out);
		}
		String next(){
			while(st==null || !st.hasMoreElements()){
				try{
					st= new StringTokenizer(br.readLine());
				}
				catch (IOException e){
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}
		int nextInt(){
			return Integer.parseInt(next());
		}
		long nextLong(){
			return Long.parseLong(next());
		}
		double nextDouble(){
			return Double.parseDouble(next());
		}
		String nextLine(){
			String str = "";
			try{
				str=br.readLine();
			}
			catch(IOException e){
				e.printStackTrace();
			}
			return str;
		}
	}

////////////////////////////////////////////////////////////////////////////////////
 public static int countDigit(long n)
    {
        return (int)Math.floor(Math.log10(n) + 1);
    }

/////////////////////////////////////////////////////////////////////////////////////////

 public static int sumOfDigits(long n)
 {

  if( n< 0)return -1 ;

  int sum = 0;

  while( n > 0)
  {
      sum = sum + (int)( n %10) ;

      n /= 10 ;
  }

  return sum ;

 }

 //////////////////////////////////////////////////////////////////////////////////////////////////

public static long arraySum(int[] arr , int start , int end)
{
    long ans = 0 ;

    for(int i = start ; i <= end  ; i++)ans += arr[i] ;

    return ans  ;
}

/////////////////////////////////////////////////////////////////////////////////

public static int mod(int x)
{
      if(x <0)return -1*x ;
      else return x ;
}
public static long mod(long x)
{
      if(x <0)return -1*x ;
      else return x ;
}

////////////////////////////////////////////////////////////////////////////////

public static void swapArray(int[] arr , int start , int end)
{
    while(start < end)
    {
        int temp = arr[start] ;
        arr[start] = arr[end];
        arr[end] = temp;
        start++ ;end-- ;
    }
}

//////////////////////////////////////////////////////////////////////////////////

public static int[][] rotate(int[][] input){

int n =input.length;
int m = input[0].length ;
int[][] output = new int [m][n];

for (int i=0; i<n; i++)
	for (int j=0;j<m; j++)
		output [j][n-1-i] = input[i][j];
return output;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////

public static int countBits(long n)
    {
        int count = 0;
        while (n != 0)
        {
            count++;
            n = (n) >> (1L) ;
        }

        return count;

    }
/////////////////////////////////////////// ////////////////////////////////////////////////

public static boolean isPowerOfTwo(long n)
{
    if(n==0)
    return false;

if(((n ) & (n-1)) == 0 ) return true ;
else return false  ;

}

/////////////////////////////////////////////////////////////////////////////////////

public static int  min(int a ,int b , int c, int d)
{
     int[] arr = new int[4] ;
      arr[0] = a;arr[1] = b ;arr[2] = c;arr[3] = d;Arrays.sort(arr) ;

      return arr[0];
}
 /////////////////////////////////////////////////////////////////////////////
public static int  max(int a ,int b , int c, int d)
{
     int[] arr = new int[4] ;
      arr[0] = a;arr[1] = b ;arr[2] = c;arr[3] = d;Arrays.sort(arr) ;

      return arr[3];
}

///////////////////////////////////////////////////////////////////////////////////

public static String reverse(String input)
{
  StringBuilder str  = new StringBuilder("") ;

    for(int i =input.length()-1 ; i >= 0  ; i-- )
    {
        str.append(input.charAt(i));
    }

return str.toString() ;
}
///////////////////////////////////////////////////////////////////////////////////////////

public static boolean sameParity(long a  ,long b )
{
     long x = a% 2L; long y = b%2L ;
     if(x==y)return true  ;
     else return false  ;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
public static boolean isPossibleTriangle(int a  ,int b , int c)
{
      if( a + b > c && c+b > a && a +c > b)return true  ;
      else return false  ;
}

////////////////////////////////////////////////////////////////////////////////////////////
static long xnor(long num1, long num2) {
		if (num1 < num2) {
			long temp = num1;
			num1 = num2;
			num2 = temp;
		}
		num1 = togglebit(num1);
		return num1 ^ num2;
	}

	static long togglebit(long n) {
		if (n == 0)
			return 1;
		long i = n;
		n |= n >> 1;
		n |= n >> 2;
		n |= n >> 4;
		n |= n >> 8;
		n |= n >> 16;
		return i ^ n;
	}

///////////////////////////////////////////////////////////////////////////////////////////////

public static int xorOfFirstN(int n)
{

 if( n % 4 ==0)return n ;

 else if( n % 4 == 1)return 1 ;

 else if( n % 4 == 2)return n+1 ;

 else return 0 ;

}
//////////////////////////////////////////////////////////////////////////////////////////////

public static int gcd(int a, int b )
{

if(b==0)return a ;

else return gcd(b,a%b) ;

}

public static long gcd(long a, long b )
{

if(b==0)return a ;

else return gcd(b,a%b) ;

}

////////////////////////////////////////////////////////////////////////////////////

public static int lcm(int a, int b ,int c , int d )
{

int temp = lcm(a,b , c) ;

 int ans = lcm(temp ,d ) ;

return ans  ;

}

///////////////////////////////////////////////////////////////////////////////////////////

public static int lcm(int a, int b ,int c )
{

int temp = lcm(a,b) ;

int ans =  lcm(temp ,c) ;

return ans  ;

}

////////////////////////////////////////////////////////////////////////////////////////

public static int lcm(int a , int b )
{

int gc = gcd(a,b);

return (a*b)/gc ;
}

public static long lcm(long a , long b )
{

long gc = gcd(a,b);

return (a*b)/gc ;
}

///////////////////////////////////////////////////////////////////////////////////////////
static boolean isPrime(long n)
{
      if(n==1)
      {
            return false  ;
      }

      boolean ans =  true  ;

      for(long i = 2L; i*i <= n ;i++)
      {
            if(n% i ==0)
            {
                  ans = false  ;break ;
            }
      }

      return ans  ;
}
///////////////////////////////////////////////////////////////////////////

static int sieve =  1000000 ;

static boolean[] prime =  new boolean[sieve + 1] ;

public static void sieveOfEratosthenes()
    {
        // FALSE == prime

        // TRUE ==  COMPOSITE

        // FALSE== 1

        // time complexity = 0(NlogLogN)== o(N)

        // gives prime nos bw 1 to N

        for(int i = 4; i<= sieve ; i++)
        {
            prime[i] = true  ;
            i++ ;
        }

        for(int p = 3; p*p <= sieve; p++)
        {

            if(prime[p] == false)
            {

                for(int i = p*p; i <= sieve; i += p)
                    prime[i] = true;
            }

            p++ ;
        }

    }

///////////////////////////////////////////////////////////////////////////////////

public static void sortD(int[] arr , int s  , int e)
{
      sort(arr ,s , e) ;

      int i =s ; int j = e  ;

      while( i < j)
      {
            int temp = arr[i] ;
            arr[i] =arr[j] ;
            arr[j] = temp ;
            i++ ; j-- ;
      }

      return ;
}

/////////////////////////////////////////////////////////////////////////////////////////

public static long countSubarraysSumToK(long[] arr ,long sum )
    {
      HashMap<Long,Long> map = new HashMap<>() ;

      int n = arr.length ;

      long prefixsum = 0 ;

      long count = 0L ;
      for(int i  = 0; i < n ; i++)
      {
          prefixsum  = prefixsum +  arr[i] ;

          if(sum == prefixsum)count = count+1 ;

          if(map.containsKey(prefixsum -sum))
          {
              count = count + map.get(prefixsum -sum) ;
          }

          if(map.containsKey(prefixsum ))
          {
              map.put(prefixsum , map.get(prefixsum) +1 );
          }

          else{
              map.put(prefixsum , 1L );
          }

      }

      return count  ;

    }

///////////////////////////////////////////////////////////////////////////////////////////////

// KMP ALGORITHM : TIME COMPL:O(N+M)
// FINDS THE OCCURENCES OF PATTERN AS A  SUBSTRING IN STRING
//RETURN THE ARRAYLIST OF INDEXES
// IF SIZE OF LIST IS ZERO MEANS PATTERN IS NOT PRESENT IN STRING

public static ArrayList<Integer> kmpAlgorithm(String str , String pat)
     {
        ArrayList<Integer> list =new ArrayList<>();

        int n = str.length() ;
        int m = pat.length() ;

        String q = pat + "#" + str ;

        int[] lps  =new int[n+m+1] ;

         longestPefixSuffix(lps, q,(n+m+1)) ;

         for(int i =m+1 ; i < (n+m+1) ; i++ )
         {
             if(lps[i] == m)
             {
                 list.add(i-2*m) ;
             }
         }

        return list ;

    }

public static void longestPefixSuffix(int[] lps ,String str , int n)
    {
        lps[0] =  0 ;

        for(int i = 1  ; i<= n-1; i++)
        {
          int l = lps[i-1] ;

          while( l > 0 && str.charAt(i) != str.charAt(l))
          {
              l = lps[l-1] ;
          }

          if(str.charAt(i) == str.charAt(l))
          {
              l++ ;
          }

          lps[i] = l ;
        }

    }

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

 // CALCULATE TOTIENT Fn FOR ALL VALUES FROM 1 TO n
    // TOTIENT(N) = count of nos less than n and grater than 1 whose gcd with n is 1
    // or n and the no  will be coprime in nature
    //time : O(n*(log(logn)))

    public static void eulerTotientFunction(int[] arr ,int n )
    {

      for(int i = 1; i <= n  ;i++)arr[i] =i  ;

      for(int i= 2 ; i<= n ;i++)
      {
          if(arr[i] == i)
          {
              arr[i] =i-1 ;

              for(int j =2*i ; j<= n  ; j+= i )
              {
                  arr[j] = (arr[j]*(i-1))/i ;
              }

          }
      }

      return  ;

    }

/////////////////////////////////////////////////////////////////////////////////////////////
public static long nCr(int n,int k)
{
    long ans=1L;
    k=k>n-k?n-k:k;
    int j=1;
    for(;j<=k;j++,n--)
    {
        if(n%j==0)
        {
            ans*=n/j;
        }else
        if(ans%j==0)
        {
            ans=ans/j*n;
        }else
        {
            ans=(ans*n)/j;
        }
    }
    return ans;
}

///////////////////////////////////////////////////////////////////////////////////////////

public static ArrayList<Integer> allFactors(int n)
{
      ArrayList<Integer> list = new ArrayList<>() ;

    for(int i = 1; i*i <= n ;i++)
    {
          if( n % i == 0)
          {
              if(i*i == n)
              {
                    list.add(i) ;
              }
              else{
                    list.add(i) ;
                    list.add(n/i) ;

              }
          }
    }

     return list ;

}

public static ArrayList<Long> allFactors(long n)
{
      ArrayList<Long> list = new ArrayList<>() ;

    for(long i = 1L; i*i <= n ;i++)
    {
          if( n % i == 0)
          {
              if(i*i == n)
              {
                    list.add(i) ;
              }
              else{
                    list.add(i) ;
                    list.add(n/i) ;

              }
          }
    }

     return list ;

}
////////////////////////////////////////////////////////////////////////////////////////////////////

  static final int MAXN = 10000001;

    static int spf[] = new int[MAXN];

    static void sieve()
    {
        spf[1] = 1;
        for (int i=2; i<MAXN; i++)

            spf[i] = i;

        for (int i=4; i<MAXN; i+=2)
            spf[i] = 2;

        for (int i=3; i*i<MAXN; i++)
        {

            if (spf[i] == i)
            {

                for (int j=i*i; j<MAXN; j+=i)

                    if (spf[j]==j)
                        spf[j] = i;
            }
        }
    }

// The above code works well for n upto the order of 10^7.
// Beyond this we will face memory issues.

// Time Complexity: The precomputation for smallest prime factor is done in O(n log log n)
// using sieve.
// Where as in the calculation step we are dividing the number every time by
// the smallest prime number till it becomes 1.
// So, let’s consider a worst case in which every time the SPF is 2 .
// Therefore will have log n division steps.

// Hence, We can say that our Time Complexity will be O(log n) in worst case.

    static Vector<Integer> getFactorization(int x)
    {
        Vector<Integer> ret = new Vector<>();
        while (x != 1)
        {
            ret.add(spf[x]);
            x = x / spf[x];
        }
        return ret;
    }

 //////////////////////////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////////////////////////////

public static void merge(int arr[], int l, int m, int r)
    {
        // Find sizes of two subarrays to be merged
        int n1 = m - l + 1;
        int n2 = r - m;

        /* Create temp arrays */
        int L[] = new int[n1];
        int R[] = new int[n2];

      //Copy data to temp arrays
        for (int i=0; i<n1; ++i)
            L[i] = arr[l + i];
        for (int j=0; j<n2; ++j)
            R[j] = arr[m + 1+ j];

        /* Merge the temp arrays */

        // Initial indexes of first and second subarrays
        int i = 0, j = 0;

        // Initial index of merged subarry array
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        /* Copy remaining elements of L[] if any */
        while (i < n1)
        {
            arr[k] = L[i];
            i++;
            k++;
        }

        /* Copy remaining elements of R[] if any */
        while (j < n2)
        {
            arr[k] = R[j];
            j++;
            k++;
        }
    }

    // Main function that sorts arr[l..r] using
    // merge()
  public static void sort(int arr[], int l, int r)
    {
        if (l < r)
        {
            // Find the middle point
            int m = (l+r)/2;

            // Sort first and second halves
            sort(arr, l, m);
            sort(arr , m+1, r);

            // Merge the sorted halves
            merge(arr, l, m, r);
        }
    }

public static void sort(long arr[], int l, int r)
    {
        if (l < r)
        {
            // Find the middle point
            int m = (l+r)/2;

            // Sort first and second halves
            sort(arr, l, m);
            sort(arr , m+1, r);

            // Merge the sorted halves
            merge(arr, l, m, r);
        }
    }

public static void merge(long arr[], int l, int m, int r)
    {
        // Find sizes of two subarrays to be merged
        int n1 = m - l + 1;
        int n2 = r - m;

        /* Create temp arrays */
        long L[] = new long[n1];
        long R[] = new long[n2];

        //Copy data to temp arrays
        for (int i=0; i<n1; ++i)
            L[i] = arr[l + i];
        for (int j=0; j<n2; ++j)
            R[j] = arr[m + 1+ j];

        /* Merge the temp arrays */

        // Initial indexes of first and second subarrays
        int i = 0, j = 0;

        // Initial index of merged subarry array
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        /* Copy remaining elements of L[] if any */
        while (i < n1)
        {
            arr[k] = L[i];
            i++;
            k++;
        }

        /* Copy remaining elements of R[] if any */
        while (j < n2)
        {
            arr[k] = R[j];
            j++;
            k++;
        }
    }

 /////////////////////////////////////////////////////////////////////////////////////////

	public static long knapsack(int[] weight,long value[],int maxWeight){

        int n=  value.length ;

	//dp[i] stores the profit with KnapSack capacity "i"
  long []dp = new long[maxWeight+1];

    //initially profit with 0 to W KnapSack capacity is 0
    Arrays.fill(dp, 0);

    // iterate through all items
    for(int i=0; i < n; i++)

        //traverse dp array from right to left
        for(int j = maxWeight; j >= weight[i]; j--)
            dp[j] = Math.max(dp[j] , value[i] + dp[j - weight[i]]);

    /*above line finds out maximum of dp[j](excluding ith element value)
    and val[i] + dp[j-wt[i]] (including ith element value and the
    profit with "KnapSack capacity - ith element weight") */
    return dp[maxWeight];
	}

///////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////

// to return max sum of any subarray in given array
public static long kadanesAlgorithm(long[] arr)
{
      long[] dp = new long[arr.length] ;

      dp[0] = arr[0] ;
      long max =  dp[0] ;

      for(int i = 1; i <  arr.length ; i++)
      {
            if(dp[i-1] > 0)
            {
                  dp[i] = dp[i-1] + arr[i] ;
            }
            else{
                  dp[i] = arr[i] ;
            }

            if(dp[i] >  max)max = dp[i] ;

      }

      return max  ;

}
/////////////////////////////////////////////////////////////////////////////////////////////
public static long kadanesAlgorithm(int[] arr)
{
      long[] dp = new long[arr.length] ;

      dp[0] = arr[0] ;
      long max =  dp[0] ;

      for(int i = 1; i <  arr.length ; i++)
      {
            if(dp[i-1] > 0)
            {
                  dp[i] = dp[i-1] + arr[i] ;
            }
            else{
                  dp[i] = arr[i] ;
            }

            if(dp[i] >  max)max = dp[i] ;

      }

      return max  ;

}

///////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////

public static long binarySerachGreater(int[] arr , int start , int end , int val)
{

      // fing total no of elements strictly grater than val in sorted array arr

      if(start >  end)return  0 ; //Base case

      int mid = (start + end)/2  ;

      if(arr[mid] <=val)
      {
          return binarySerachGreater(arr,mid+1, end ,val) ;

      }
      else{

         return binarySerachGreater(arr,start , mid -1,val) + end-mid+1 ;

      }

}

//////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

//TO GENERATE ALL(DUPLICATE ALSO EXIST) PERMUTATIONS OF A STRING

// JUST CALL generatePermutation( str,  start,  end) start :inclusive ,end : exclusive

//Function for swapping the characters at position I with character at position j
    public static String swapString(String a, int i, int j) {
        char[] b =a.toCharArray();
        char ch;
        ch = b[i];
        b[i] = b[j];
        b[j] = ch;
        return String.valueOf(b);
    }

//Function for generating different permutations of the string
    public static void generatePermutation(String str, int start, int end)
    {
        //Prints the permutations
        if (start == end-1)
            System.out.println(str);
        else
        {
            for (int i = start; i < end; i++)
            {
                //Swapping the string by fixing a character
                str = swapString(str,start,i);
                //Recursively calling function generatePermutation() for rest of the characters
                generatePermutation(str,start+1,end);
                //Backtracking and swapping the characters again.
                str = swapString(str,start,i);
            }
        }
    }

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////

public static long factMod(long n, long mod) {
    if (n <= 1) return 1;
    long ans = 1;
    for (int i = 1; i <= n; i++) {
      ans = (ans * i) % mod;
    }
    return ans;
  }

/////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////

public static long power(long x  ,long n)
    {
        //time comp : o(logn)

        if(n==0)return 1L ;
        if(n==1)return x;

        long ans =1L  ;

      while(n>0)
      {
          if(n % 2 ==1)
          {
              ans = ans *x ;
          }

          n /= 2 ;

          x =  x*x ;

      }

      return ans ;
    }

////////////////////////////////////////////////////////////////////////////////////////////////////
public static long powerMod(long x, long n, long mod) {
    //time comp : o(logn)

    if(n==0)return 1L ;
        if(n==1)return x;

    long ans = 1;
    while (n > 0) {
      if (n % 2 == 1) ans = (ans * x) % mod;
      x = (x * x) % mod;
      n /= 2;
    }
    return ans;
  }

//////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////

/*
lowerBound - finds largest element equal or less than value paased
upperBound - finds smallest element equal or more than value passed

if not present return -1;

*/

public static long lowerBound(long[] arr,long k)
	{
		long ans=-1;

		int start=0;
		int end=arr.length-1;

		while(start<=end)
		{
			int mid=(start+end)/2;

			if(arr[mid]<=k)
			{
				ans=arr[mid];
				start=mid+1;
			}
			else
			{
				end=mid-1;
			}

		}

		return ans;

	}

	public static int lowerBound(int[] arr,int k)
	{
		int ans=-1;

		int start=0;
		int end=arr.length-1;

		while(start<=end)
		{
			int mid=(start+end)/2;

			if(arr[mid]<=k)
			{
				ans=arr[mid];
				start=mid+1;
			}
			else
			{
				end=mid-1;
			}

		}

		return ans;

	}

	public static long upperBound(long[] arr,long k)
	{
		long ans=-1;

		int start=0;
		int end=arr.length-1;

		while(start<=end)
		{
			int mid=(start+end)/2;

			if(arr[mid]>=k)
			{
				ans=arr[mid];
				end=mid-1;
			}
			else
			{
				start=mid+1;
			}

		}

		return ans;
	}

	public static int upperBound(int[] arr,int k)
	{
		int ans=-1;

		int start=0;
		int end=arr.length-1;

		while(start<=end)
		{
			int mid=(start+end)/2;

			if(arr[mid]>=k)
			{
				ans=arr[mid];
				end=mid-1;
			}
			else
			{
				start=mid+1;
			}

		}

		return ans;
	}

//////////////////////////////////////////////////////////////////////////////////////////

public static void printArray(int[] arr , int si ,int ei)
{
    for(int i = si  ; i  <= ei ; i++)
    {
        out.print(arr[i] +" ") ;
    }

}

public static void printArrayln(int[] arr , int si ,int ei)
{
    for(int i = si  ; i  <= ei ; i++)
    {
        out.print(arr[i] +" ") ;
    }
    out.println() ;
}

public static void printLArray(long[] arr , int si , int ei)
{
    for(int i = si ; i  <= ei ; i++)
    {
        out.print(arr[i] +" ") ;
    }

}

public static void printLArrayln(long[] arr , int si , int ei)
{
    for(int i = si ; i  <= ei ; i++)
    {
        out.print(arr[i] +" ") ;
    }
    out.println() ;

}

public static void printtwodArray(int[][] ans)
{
    for(int i = 0; i< ans.length ; i++)
    {
        for(int j  = 0 ; j <  ans[0].length ; j++)out.print(ans[i][j] +" ");
        out.println() ;
    }
    out.println() ;

}

  static long modPow(long a, long x, long p) {
    //calculates a^x mod p in logarithmic time.
    long res = 1;
    while(x > 0) {
        if( x % 2 != 0) {
            res = (res * a) % p;
        }
        a = (a * a) % p;
        x /= 2;
    }
    return res;
}

  static long modInverse(long a, long p) {
    //calculates the modular multiplicative of a mod m.
    //(assuming p is prime).
    return modPow(a, p-2, p);
}

static long modBinomial(long n, long k, long p) {
// calculates C(n,k) mod p (assuming p is prime).

    long numerator = 1; // n * (n-1) * ... * (n-k+1)
    for (int i=0; i<k; i++) {
        numerator = (numerator * (n-i) ) % p;
    }

    long denominator = 1; // k!
    for (int i=1; i<=k; i++) {
        denominator = (denominator * i) % p;
    }

    // numerator / denominator mod p.
    return ( numerator* modInverse(denominator,p) ) % p;
}

/////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////

 static ArrayList<Integer>[] tree ;
// static long[] child;
// static int mod= 1000000007 ;
// static int[][] pre  = new int[3001][3001];
// static int[][] suf = new int[3001][3001] ;

//program to calculate noof nodes in subtree for every vertex including itself

 public static void countNoOfNodesInsubtree(int child ,int par , int[] dp)
 {
     int count = 1 ;

     for(int x : tree[child])
     {

         if(x== par)continue  ;

         countNoOfNodesInsubtree(x,child,dp) ;

         count= count +  dp[x]   ;
     }

     dp[child] = count ;

 }

public static void depth(int child ,int par , int[] dp , int d )
 {
    dp[child] =d ;

     for(int x : tree[child])
     {

         if(x== par)continue  ;

         depth(x,child,dp,d+1) ;

     }

 }

public static void dfs(int sv , boolean[] vis)
{
    vis[sv] = true  ;

    for(int x : tree[sv])
    {
        if( !vis[x])
        {
            dfs(x ,vis) ;
        }
    }

}

//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////

public static void solve()
{
FastReader scn = new FastReader() ;

//Scanner scn = new Scanner(System.in);
//int[] store = {2 ,3, 5 , 7  ,11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 } ;

// product of first 11 prime nos is greater than 10 ^ 12;

//ArrayList<Integer> arr[] = new ArrayList[n] ;
ArrayList<Integer> list = new ArrayList<>() ;
ArrayList<Long> listl = new ArrayList<>() ;
ArrayList<Integer> lista = new ArrayList<>() ;
ArrayList<Integer> listb = new ArrayList<>() ;
//ArrayList<String> lists = new ArrayList<>() ;

HashMap<Integer,Integer> map = new HashMap<>() ;
//HashMap<Long,Long> map = new HashMap<>() ;
HashMap<Integer,Integer> map1  = new HashMap<>() ;
HashMap<Integer,Integer> map2  = new HashMap<>() ;
//HashMap<String,Integer> maps = new HashMap<>() ;
//HashMap<Integer,Boolean> mapb = new HashMap<>() ;
//HashMap<Point,Integer> point = new HashMap<>() ;

 Set<Integer> set = new HashSet<>() ;
 Set<Integer> setx = new HashSet<>() ;
 Set<Integer> sety = new HashSet<>() ;

StringBuilder sb =new StringBuilder("") ;

//Collections.sort(list);

//if(map.containsKey(arr[i]))map.put(arr[i] , map.get(arr[i]) +1 ) ;
//else map.put(arr[i],1) ;

// if(map.containsKey(temp))map.put(temp , map.get(temp) +1 ) ;
// else map.put(temp,1) ;

//int bit  =Integer.bitCount(n);
// gives total no of set bits in n;

// Arrays.sort(arr, new Comparator<Pair>() {
// 			@Override
// 			public int compare(Pair a, Pair b) {
// 				if (a.first != b.first) {
// 					return a.first - b.first; // for increasing order of first
// 				}
// 				return a.second - b.second ; //if first is same then sort on second basis
// 			}
// 		});

int testcases = 1;
 //testcases = scn.nextInt() ;
for(int testcase =1  ; testcase <= testcases ;testcase++)
{

 //if(map.containsKey(arr[i]))map.put(arr[i],map.get(arr[i])+1) ;else map.put(arr[i],1) ;
 //if(map.containsKey(temp))map.put(temp,map.get(temp)+1) ;else map.put(temp,1) ;

//  int n = scn.nextInt() ;int m = scn.nextInt() ;
// tree = new ArrayList[n] ;
// for(int i = 0; i< n; i++)
// {
//     tree[i] = new ArrayList<Integer>();
// }

//  for(int i = 0 ;  i <= m-1 ; i++)
// {
//     int fv = scn.nextInt()-1 ; int sv = scn.nextInt()-1 ;
//     tree[fv].add(sv) ;
//     tree[sv].add(fv) ;
// }
// boolean[] visited = new boolean[n] ;
// int ans = 0 ;
// for(int i = 0 ; i < n ; i++)
// {
//     if(!visited[i])
//     {
//         dfs(i , visited ) ;
//         ans++ ;
//     }
// }

long n = scn.nextLong() ;

long a1 = scn.nextLong() ;long a2 = scn.nextLong() ;long a3 = scn.nextLong() ;
long b1 = scn.nextLong() ;long b2 = scn.nextLong() ;long b3 = scn.nextLong() ;

long min = 0L ;

if(b1 >= a1+a2)
{
    min = a3-b2-b3 ;
}

else if(b2 >= a3+a2)
{
    min = a1-b1-b3 ;
}

else if(b3 >= a1+a3)
{
    min =a2-b2-b1 ;
}

else{
    min = 0 ;
}

long max = Math.min(a1,b2) + Math.min(a2,b3) +Math.min(a3,b1) ;

out.println(min+" " + max) ;

sb.delete(0 , sb.length()) ;
list.clear() ;listb.clear() ;
map.clear() ;
map1.clear() ;
map2.clear() ;
set.clear() ;sety.clear() ;

} // test case  end loop

out.flush() ;
} // solve fn ends

public static void main (String[] args) throws java.lang.Exception
{

solve() ;

}

}

 class Pair
{
  int first ;

  int second  ;

@Override
	public String toString() {

	String ans = "" ;
	ans  += this.first ;
	ans += " ";
	ans += this.second ;

	return ans  ;
	}

}


import java.util.ArrayList;
import java.util.HashSet;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.Random;
import java.io.FileWriter;
import java.io.PrintWriter;
/*
    Solution Created: 15:30:24 28/09/2020
    Custom Competitive programming helper.
*/

public class Main {
	static ArrayList<Integer> ans;
	public static void solve(Reader in, Writer out) {
		int t = 1;
		while(t-->0) {
			int n = in.nextInt();
			int[] a = in.na(3);
			int[] b = in.na(3);
			int v2 = s(a,b);
			fix(a, b);
			int v1 = s(a,b);
			out.println(v1+" "+v2);
		}
	}
	public static void fix(int[] a, int[] b) {
		for(int i = 0; i<3*5; i++) {
			int t1 = (i%3), t2 = (i+2)%3;
			if(b[t1]>b[t2]) {
				int mn = Math.min(b[t1], a[i%3]);
				a[i%3]-=mn;
				b[t1]-=mn;
			}else {
				int mn = Math.min(b[t2], a[i%3]);
				a[i%3]-=mn;
				b[t2]-=mn;
			}
		}
	}
	public static int s(int[] a, int[] b) {
		int ans = Math.min(a[0], b[1]);
		ans += Math.min(a[1], b[2]);
		ans += Math.min(a[2], b[0]);
		return ans;
	}
	public static void main(String[] args) {
		Reader in = new Reader();
		Writer out = new Writer();
		solve(in, out);
		out.exit();
	}
}
/*

public class Main {
	static ArrayList<Integer> ans;
	public static void solve(Reader in, Writer out) {
		int t = 1;
		while(t-->0) {
			int n = in.nextInt();
			int[] a = in.na(3);
			int[] b = in.na(3);
			out.println(f(a, b)+" "+s(a,b));
		}
	}
	public static int f(int[] a, int[] b) {
		//r s p
		int ans = 0;
		for(int i = 0; i<9; i++) {
			int t1 = (i%3), t2 = (i+2)%3;
			if(b[t1]>b[t2]) {
				int mn = Math.min(b[t1], a[i%3]);
				a[i%3]-=mn;
				b[t1]-=mn;
			}else {
				int mn = Math.min(b[t2], a[i%3]);
				a[i%3]-=mn;
				b[t2]-=mn;
			}
		}
		Util.dbg(a);
		Util.dbg(b);
		return ans;
	}
	public static int s(int[] a, int[] b) {
		int ans = Math.min(a[0], b[1]);
		ans += Math.min(a[1], b[2]);
		ans += Math.min(a[2], b[0]);
		return ans;
	}
	public static void main(String[] args) {
		Reader in = new Reader();
		Writer out = new Writer();
		solve(in, out);
		out.exit();
	}

static class Reader {
	static BufferedReader br;
	static StringTokenizer st;
	private int charIdx = 0;
	private String s;

	public Reader() {
		this.br = new BufferedReader(new InputStreamReader(System.in));
	}

	public Reader(String f){
		try {
			this.br = new BufferedReader(new FileReader(f));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public int[] na(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = nextInt();
		return a;
	}

	public double[] nd(int n) {
		double[] a = new double[n];
		for (int i = 0; i < n; i++)
			a[i] = nextDouble();
		return a;
	}

	public char nextChar() {
		if (s == null || charIdx >= s.length()) {
			if (st == null || !st.hasMoreTokens())
				try {
					readLine();
				} catch (Exception e) {
				}
			s = st.nextToken();
			charIdx = 0;
		}
		return s.charAt(charIdx++);
	}

	public long[] nl(int n) {
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = nextLong();
		return a;
	}

	public char[] nca() {
		return next().toCharArray();
	}

	public String[] nS(int n) {
		String[] a = new String[n];
		for (int i = 0; i < n; i++)
			a[i] = next();
		return a;
	}

	public int nextInt() {
		if (st == null || !st.hasMoreTokens())
			try {
				readLine();
			} catch (Exception e) {
			}
		return Integer.parseInt(st.nextToken());
	}

	public double nextDouble() {
		if (st == null || !st.hasMoreTokens())
			try {
				readLine();
			} catch (Exception e) {
			}
		return Double.parseDouble(st.nextToken());
	}

	public Long nextLong() {
		if (st == null || !st.hasMoreTokens())
			try {
				readLine();
			} catch (Exception e) {
			}
		return Long.parseLong(st.nextToken());
	}

	public String next() {
		if (st == null || !st.hasMoreTokens())
			try {
				readLine();
			} catch (Exception e) {
			}
		return st.nextToken();
	}

	public static void readLine() {
		try {
			st = new StringTokenizer(br.readLine());
		} catch (Exception e) {
		}
	}
}

static class Util{
		private static Random random = new Random();

		public static long modInverse(long a, long MOD) {
			long[] gcdE = gcdExtended(a, MOD);
			if (gcdE[0] != 1) return -1; // Inverted doesn't exist
			long x = gcdE[1];
			return (x % MOD + MOD) % MOD;
		}
		public static long[] gcdExtended(long p, long q) {
			if (q == 0) return new long[] { p, 1, 0 };
			long[] vals = gcdExtended(q, p % q);
			long tmp = vals[2];
			vals[2] = vals[1] - (p / q) * vals[2];
			vals[1] = tmp;
			return vals;
		}
		public void fac(int n) {

		}
		public static boolean isPrime(int n) {
	        if (n <= 1) return false;
	        if (n <= 3) return true;
	        if (n % 2 == 0 || n % 3 == 0) return false;
	        for (int i = 5; i * i <= n; i = i + 6)
	            if (n % i == 0 || n % (i + 2) == 0)
	            return false;
	        return true;
	    }
	    public static int lowerBound(int[] a, int v) {
	    	int l = 0, h = a.length;
	    	while(l<h) {
	    		int mid = (l+h)/2;
	    		if(v<=a[mid]) h = mid;
	    		else l = mid+1;
	    	}
	    	return l;
	    }
	    public static int lowerBound(long[] a, long v) {
	    	int l = 0, h = a.length;
	    	while(l<h) {
	    		int mid = (l+h)/2;
	    		if(v<=a[mid]) h = mid;
	    		else l = mid+1;
	    	}
	    	return l;
	    }
	    public static int upperBound(int[] a, int v) {
	    	int l = 0, h = a.length;
	    	while(l<h) {
	    		int mid = (l+h)/2;
	    		if(a[mid]<=v) l = mid+1;
	    		else h = mid;
	    	}
	    	return l;
	    }
	    public static int upperBound(long[] a, long v) {
	    	int l = 0, h = a.length;
	    	while(l<h) {
	    		int mid = (l+h)/2;
	    		if(a[mid]<=v) l = mid+1;
	    		else h = mid;
	    	}
	    	return l;
	    }
	    public static boolean[] getSieve(int n) {
	        boolean[] isPrime = new boolean[n+1];
	        for (int i = 2; i <= n; i++) isPrime[i] = true;
	        for (int i = 2; i*i <= n; i++) if (isPrime[i])
	                for (int j = i; i*j <= n; j++) isPrime[i*j] = false;
	        return isPrime;
	    }
	    public static int gcd(int a, int b) {
	    	int tmp = 0;
	    	while(b != 0) {
	    		tmp = b;
	    		b = a%b;
	    		a = tmp;
	    	}
	    	return a;
	    }
	    public static long gcd(long a, long b) {
	    	long tmp = 0;
	    	while(b != 0) {
	    		tmp = b;
	    		b = a%b;
	    		a = tmp;
	    	}
	    	return a;
	    }
	    public static int random(int min, int max) {
	    	return random.nextInt(max-min+1)+min;
	    }
		public static void dbg(Object... o) {
			System.out.println(Arrays.deepToString(o));
		}
		public static void reverse(int[] s, int l , int r) {
			for(int i = l; i<=(l+r)/2; i++) {
				int tmp = s[i];
				s[i] = s[r+l-i];
				s[r+l-i] = tmp;
			}
	    }
		public static void reverse(int[] s) {
			reverse(s, 0, s.length-1);
	    }
		public static void reverse(long[] s, int l , int r) {
			for(int i = l; i<=(l+r)/2; i++) {
				long tmp = s[i];
				s[i] = s[r+l-i];
				s[r+l-i] = tmp;
			}
	    }
		public static void reverse(long[] s) {
			reverse(s, 0, s.length-1);
	    }
		public static void reverse(float[] s, int l , int r) {
			for(int i = l; i<=(l+r)/2; i++) {
				float tmp = s[i];
				s[i] = s[r+l-i];
				s[r+l-i] = tmp;
			}
	    }
		public static void reverse(float[] s) {
			reverse(s, 0, s.length-1);
	    }
		public static void reverse(double[] s, int l , int r) {
			for(int i = l; i<=(l+r)/2; i++) {
				double tmp = s[i];
				s[i] = s[r+l-i];
				s[r+l-i] = tmp;
			}
	    }
		public static void reverse(double[] s) {
			reverse(s, 0, s.length-1);
	    }
		public static void reverse(char[] s, int l , int r) {
			for(int i = l; i<=(l+r)/2; i++) {
				char tmp = s[i];
				s[i] = s[r+l-i];
				s[r+l-i] = tmp;
			}
	    }
		public static void reverse(char[] s) {
			reverse(s, 0, s.length-1);
	    }
		public static <T> void reverse(T[] s, int l , int r) {
			for(int i = l; i<=(l+r)/2; i++) {
				T tmp = s[i];
				s[i] = s[r+l-i];
				s[r+l-i] = tmp;
			}
	    }
		public static <T> void reverse(T[] s) {
			reverse(s, 0, s.length-1);
	    }
		public static void shuffle(int[] s) {
	        for (int i = 0; i < s.length; ++i) {
	            int j = random.nextInt(i + 1);
	            int t = s[i];
	            s[i] = s[j];
	            s[j] = t;
	        }
	    }
	    public static void shuffle(long[] s) {
	        for (int i = 0; i < s.length; ++i) {
	            int j = random.nextInt(i + 1);
	            long t = s[i];
	            s[i] = s[j];
	            s[j] = t;
	        }
	    }
	    public static void shuffle(float[] s) {
	        for (int i = 0; i < s.length; ++i) {
	            int j = random.nextInt(i + 1);
	            float t = s[i];
	            s[i] = s[j];
	            s[j] = t;
	        }
	    }
	    public static void shuffle(double[] s) {
	        for (int i = 0; i < s.length; ++i) {
	            int j = random.nextInt(i + 1);
	            double t = s[i];
	            s[i] = s[j];
	            s[j] = t;
	        }
	    }
	    public static void shuffle(char[] s) {
	        for (int i = 0; i < s.length; ++i) {
	            int j = random.nextInt(i + 1);
	            char t = s[i];
	            s[i] = s[j];
	            s[j] = t;
	        }
	    }
	    public static <T> void shuffle(T[] s) {
	        for (int i = 0; i < s.length; ++i) {
	            int j = random.nextInt(i + 1);
	            T t = s[i];
	            s[i] = s[j];
	            s[j] = t;
	        }
	    }
		public static void sortArray(int[] a) {
	        shuffle(a);
	        Arrays.sort(a);
	    }
		public static void sortArray(long[] a) {
			shuffle(a);
	        Arrays.sort(a);
	    }
		public static void sortArray(float[] a) {
			shuffle(a);
	        Arrays.sort(a);
	    }
		public static void sortArray(double[] a) {
			shuffle(a);
	        Arrays.sort(a);
	    }
		public static void sortArray(char[] a) {
			shuffle(a);
	        Arrays.sort(a);
	    }
		public static <T extends Comparable<T>> void sortArray(T[] a) {
	        Arrays.sort(a);
	    }
	}

static class Writer {
	private PrintWriter pw;
	public Writer(){
		pw = new PrintWriter(System.out);
	}

	public Writer(String f){
		try {
			pw = new PrintWriter(new FileWriter(f));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void printArray(int[] a) {
		for(int i = 0; i<a.length; i++) print(a[i]+" ");
	}
	public void printlnArray(int[] a) {
		for(int i = 0; i<a.length; i++) print(a[i]+" ");
		pw.println();
	}
	public void printArray(long[] a) {
		for(int i = 0; i<a.length; i++) print(a[i]+" ");
	}
	public void printlnArray(long[] a) {
		for(int i = 0; i<a.length; i++) print(a[i]+" ");
		pw.println();
	}
	public void print(Object o) {
		pw.print(o.toString());
	}
	public void println(Object o) {
		pw.println(o.toString());
	}
	public void println() {
		pw.println();
	}
	public void flush() {
		pw.flush();
	}
	public void exit() {
		pw.close();
	}
}
}


//package javaapplication1;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) throws IOException{
        //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(new PrintStream(System.out));
        //Scanner sc=new Scanner(System.in);
        FastScanner in=new FastScanner();
        String x=in.nextToken();
        Set<Character>s=new HashSet<>();
        for (int i = 0; i < x.length(); i++) {
            s.add(x.charAt(i));
        }
        char a=96;
        for (int i = 0; i <s.size(); i++) {
            ++a;if(!s.contains(a)){pw.println("NO");pw.flush();System.exit(0);}
        }
        int [] arr=new int[26];
        a=96;
        for (int i = 0; i < s.size(); i++) {
            ++a;
            arr[i]=x.indexOf(a);
        }
        for (int i = 0; i < s.size()-1; i++) {
            if(arr[i]>arr[i+1]){pw.println("NO");pw.flush();System.exit(0);}
        }
        pw.println("YES");pw.flush();
    }
    public static class FastScanner
	{
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(String s)
		{
			try {
				br = new BufferedReader(new FileReader(s));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		public FastScanner()
		{
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String nextToken()
		{
			while (st == null || !st.hasMoreElements())
			{
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt()
		{
			return Integer.parseInt(nextToken());
		}

		long nextLong()
		{
			return Long.parseLong(nextToken());
		}

		double nextDouble()
		{
			return Double.parseDouble(nextToken());
		}

		String next()
		{
			return nextToken();
		}
	}


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

public class HEnitA {

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    //PrintWriter w = new PrintWriter(System.out);
    char[] a=in.next().toCharArray();
    HashSet<Character> hs=new HashSet<>();
    char cur='a';
    hs.add(cur);
    int i;
    for(i=0;i<a.length;i++){
      if (!hs.contains(a[i])){
        //System.out.println("a[i] is : "+a[i]);
          //System.out.println("cur is : "+cur);
        if ((int)a[i]!=(int)cur+1){

        //  System.out.println("(int)a[i] : "+(int)a[i]);
          //System.out.println("(int)cur+1 : "+((int)cur+1));
            break;
        }
        else{
          //System.out.println("hi");
          cur=a[i];
          hs.add(cur);
        }
      }
    }
    //System.out.println("i is : "+i);
    if (i==a.length)
      System.out.println("YES");
    else
      System.out.println("NO");

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;

public class b
{
    private static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    static String readln() throws IOException
    {
        String line = input.readLine();
        return line;
    }

    public static void main(String[] args) throws IOException
    {
        Locale.setDefault(Locale.US);
        String input = a.readln();

        char[] s = input.toCharArray();
        int max = 'a' - 1;
        for(char c : s){
        	if(c > max + 1){
        		System.out.println("NO");
        		return;
        	}

        	max = Math.max(c, max);
        }

        System.out.println("YES");
    }

}


import java.util.HashMap;
import java.util.Scanner;

public class imanalog {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		int vi[] = new int[26];
		String h = "";
		for(int i = 0; i < s.length();i++){
			int ch  = s.charAt(i) - 'a';
			if(vi[ch] == 0){
				h = h + s.charAt(i);
				vi[ch] = 1;
			}

		}
			int flag = 0;
		for(int i = 1; i < h.length();  i++){
			if(h.charAt(i-1) > h.charAt(i)){
				flag = 1;
				break;
			}
		}
		if(flag == 0)
			System.out.println("YES");
		else
			System.out.println("NO");

}

}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class C_Round_90 {

    public static long MOD = 1000000007;
    static long[][][][] dp;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        int m = in.nextInt();
        int k = in.nextInt();
        long[][] data = new long[m][4];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextLong();
            }
            data[i][3] = i;
        }
        Arrays.sort(data, new Comparator<long[]>() {
            @Override
            public int compare(long[] o1, long[] o2) {
                return Long.compare(a[2], b[2]);
            }

        }
        );
        dp = new long[m][n + 1][m + 1][101];
        for (long[][][] a : dp) {
            for (long[][] b : a) {
                for (long[] c : b) {
                    Arrays.fill(c, -1);
                }
            }
        }
        long v = cal(0, n, m, 0, m, k, data);
        // out.println(v);
        if (v != -1) {
            out.println("YES");
            int index = 0;
            int left = n;
            int last = m;
            int num = 0;
            while (left > 0) {
                // System.out.println(index + " " + left + " " + last + " " + num + " " + (dp[index][left][last][num]));
                if (index + 1 < m && dp[index][left][last][num] == dp[index + 1][left][last][num]) {

                } else if (last == m) {
                    for (int i = 0; i + data[index][0] <= data[index][1]; i++) {
                        if (dp[index][left][last][num] == (long) i + data[index][0] + (index + 1 < m ? dp[index + 1][left - 1][index][i] : 0) && (index + 1 == m || dp[index + 1][left - 1][index][i] != -1)) {
                            out.println((data[index][3] + 1) + " " + (data[index][0] + i));
                            left--;
                            last = index;
                            num = i;
                            break;
                        }
                    }
                } else {
                    long tmp = data[last][0] + num;
                    if (data[index][0] <= tmp + k && data[index][1] >= tmp + k) {
                        int nxt = (int) (tmp + k - data[index][0]);
                        if ((index + 1 == m || dp[index + 1][left - 1][index][nxt] != -1) && (index + 1 < m ? dp[index + 1][left - 1][index][nxt] : 0) + tmp + k == dp[index][left][last][num]) {
                            out.println((data[index][3] + 1) + " " + (tmp + k));
                            left--;
                            last = index;
                            num = nxt;
                            index++;
                            continue;
                        }
                    }
                    int nxt = (int) (tmp * k - data[index][0]);
                    out.println((data[index][3] + 1) + " " + (tmp * k));
                    left--;
                    last = index;
                    num = nxt;

                }
                index++;
            }
        } else {
            out.println("NO");
        }
        out.close();
    }

    public static long cal(int index, int left, int last, int num, int m, long k, long[][] data) {
        if (left == 0) {
            if (index < m) {
                dp[index][left][last][num] = 0;
            }
            return 0;
        }
        if (m - index < left) {
            return -1;
        }

        if (dp[index][left][last][num] != -1) {
            return dp[index][left][last][num];
        }
        long result = cal(index + 1, left, last, num, m, k, data);

        if (last == m) {

            for (int i = 0; i <= data[index][1] - data[index][0]; i++) {
                long v = cal(index + 1, left - 1, index, i, m, k, data);
                if (v != -1) {
                    result = max(result, v + (long) i + data[index][0]);
                }
            }
        } else if (data[index][2] > data[last][2]) {
            long tmp = data[last][0] + num;
            if (data[index][0] <= tmp + k && data[index][1] >= tmp + k) {
                long v = cal(index + 1, left - 1, index, (int) (tmp + k - data[index][0]), m, k, data);
                if (v != -1) {
                    result = max(result, tmp + k + v);
                }
            }
            if (data[index][0] <= k * tmp && k * tmp <= data[index][1]) {
                long v = cal(index + 1, left - 1, index, (int) (tmp * k - data[index][0]), m, k, data);
                if (v != -1) {
                    result = max(result, tmp * k + v);
                }
            }
        }
        return dp[index][left][last][num] = result;

    }

    static long max(long a, long b) {
        return a > b ? a : b;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return x - o.x;
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    static int n = 0, m = 0, k = 0;
    public static void main(String[] args) {
        Scanner r = new Scanner(System.in);

        n = r.nextInt();
        m = r.nextInt();
        k = r.nextInt();

        a = new Subject[m];
        for(int i = 0; i < m; i++)
            a[i] = new Subject(r.nextLong(), r.nextLong(), r.nextInt(), i + 1);

        Arrays.sort(a);
        for(long[][] i : dp)
        for(long[] j : i)
            Arrays.fill(j, empty);

        long max = -inf, maxD = -1;
        int maxS = -1;
        for(int s = 0; s < m; s++)
        for(long d = a[s].a ; d <= a[s].b; d++){
            long curr = go(s, d, n);
            if(curr > max){
                max = curr;
                maxD = d;
                maxS = s;
            }
        }

        if(max < 0)System.out.println("NO");
        else{
            System.out.println("YES");

            construct(maxS, maxD, n);
        }
    }
    private static void construct(int i, long x, int rem) {
        System.out.println(a[i].i + " " + x);
        if(rem - 1 != 0)construct(S[i][(int) (x - a[i].a)][rem], X[i][(int) (x - a[i].a)][rem], rem - 1);

    }
    static Subject[] a;
    static long inf = 1L << 62;
    static long empty = -1L << 63;
    static int[][][] S = new int[60][110][60];
    static long[][][] X = new long[60][110][60];
    static long[][][] dp = new long[60][110][60];
    private static long go(int i, long x, int rem) {
        if(rem == 1){
            return x;
        }

        if(dp[i][(int) (x - a[i].a)][rem] != empty)return dp[i][(int) (x - a[i].a)][rem];

        long best = -inf;
        for(int j = i + 1; j < m; j++){
            if(a[j].c == a[i].c)continue;

            if(x + k >= 0 && a[j].a <= x + k && x + k <= a[j].b){
                long w1 = x + go(j, x + k, rem - 1);
                if(w1 > best){
                    best = w1;
                    S[i][(int) (x - a[i].a)][rem] = j;
                    X[i][(int) (x - a[i].a)][rem] = x + k;
                }
            }

            if(x * k >= 0 && a[j].a <= x * k && x * k <= a[j].b){
                long w2 = x + go(j, x * k, rem - 1);
                if(w2 > best){
                    best = w2;
                    S[i][(int) (x - a[i].a)][rem] = j;
                    X[i][(int) (x - a[i].a)][rem] = x * k;
                }
            }
        }

        return dp[i][(int) (x - a[i].a)][rem] = best;
    }
}
class Subject implements Comparable<Subject>{
    long a, b;
    int c, i;
    public Subject(long ai, long bi, int ci, int ii){
        a = ai;
        b = bi;
        c = ci;
        i = ii;
    }
    @Override
    public int compareTo(Subject b) {
        if(c < b.c)return -1;
        else if(c > b.c)return 1;
        else return 0;
    }
}
class P{
    int i;
    long t;
    public P(int ii, long ti){
        i = ii;
        t = ti;
    }
    public String toString(){
        return String.format("%d %d", i, t);
    }
}import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    static int n = 0, m = 0, k = 0;
    public static void main(String[] args) {
        Scanner r = new Scanner(System.in);

        n = r.nextInt();
        m = r.nextInt();
        k = r.nextInt();

        a = new Subject[m];
        for(int i = 0; i < m; i++)
            a[i] = new Subject(r.nextLong(), r.nextLong(), r.nextInt(), i + 1);

        Arrays.sort(a);
        for(long[][] i : dp)
        for(long[] j : i)
            Arrays.fill(j, empty);

        long max = -inf, maxD = -1;
        int maxS = -1;
        for(int s = 0; s < m; s++)
        for(long d = a[s].a ; d <= a[s].b; d++){
            long curr = go(s, d, n);
            if(curr > max){
                max = curr;
                maxD = d;
                maxS = s;
            }
        }

        if(max < 0)System.out.println("NO");
        else{
            System.out.println("YES");

            construct(maxS, maxD, n);
        }
    }
    private static void construct(int i, long x, int rem) {
        System.out.println(a[i].i + " " + x);
        if(rem - 1 != 0)construct(S[i][(int) (x - a[i].a)][rem], X[i][(int) (x - a[i].a)][rem], rem - 1);

    }
    static Subject[] a;
    static long inf = 1 << 28;
    static long empty = -1 << 29;
    static int[][][] S = new int[60][110][60];
    static long[][][] X = new long[60][110][60];
    static long[][][] dp = new long[60][110][60];
    private static long go(int i, long x, int rem) {
        if(rem == 1){
            return x;
        }

        if(dp[i][(int) (x - a[i].a)][rem] != empty)return dp[i][(int) (x - a[i].a)][rem];

        long best = -inf;
        for(int j = i + 1; j < m; j++){
            if(a[j].c == a[i].c)continue;

            if(x + k >= 0 && a[j].a <= x + k && x + k <= a[j].b){
                long w1 = x + go(j, x + k, rem - 1);
                if(w1 > best){
                    best = w1;
                    S[i][(int) (x - a[i].a)][rem] = j;
                    X[i][(int) (x - a[i].a)][rem] = x + k;
                }
            }

            if(x * k >= 0 && a[j].a <= x * k && x * k <= a[j].b){
                long w2 = x + go(j, x * k, rem - 1);
                if(w2 > best){
                    best = w2;
                    S[i][(int) (x - a[i].a)][rem] = j;
                    X[i][(int) (x - a[i].a)][rem] = x * k;
                }
            }
        }

        return dp[i][(int) (x - a[i].a)][rem] = best;
    }
}
class Subject implements Comparable<Subject>{
    long a, b;
    int c, i;
    public Subject(long ai, long bi, int ci, int ii){
        a = ai;
        b = bi;
        c = ci;
        i = ii;
    }
    @Override
    public int compareTo(Subject b) {
        if(c < b.c)return -1;
        else if(c > b.c)return 1;
        else return 0;
    }
}
class P{
    int i;
    long t;
    public P(int ii, long ti){
        i = ii;
        t = ti;
    }
    public String toString(){
        return String.format("%d %d", i, t);
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class C_Round_90 {

    public static long MOD = 1000000007;
    static long[][][][] dp;
    static HashSet<Long> set = new HashSet();

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        int m = in.nextInt();
        int k = in.nextInt();
        long[][] data = new long[m][4];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextLong();
            }
            data[i][3] = i;
        }
        Arrays.sort(data, new Comparator<long[]>() {
            @Override
            public int compare(long[] a, long[] b) {
                return Long.compare(a[2], b[2]);
            }

        }
        );

        dp = new long[m][n + 1][m + 1][101];
        for (long[][][] a : dp) {
            for (long[][] b : a) {
                for (long[] c : b) {
                    Arrays.fill(c, -1);
                }
            }
        }
        long v = cal(0, n, m, 0, m, k, data);
        // out.println(v);

        if (v != -1) {
            out.println("YES");
            int index = 0;
            int left = n;
            int last = m;
            int num = 0;
            while (left > 0) {
                // System.out.println(index + " " + left + " " + last + " " + num + " " + (dp[index][left][last][num]));
                if (index + 1 < m && dp[index][left][last][num] == dp[index + 1][left][last][num]) {

                } else if (last == m) {
                    for (int i = 0; i + data[index][0] <= data[index][1]; i++) {
                        if (dp[index][left][last][num] == (long) i + data[index][0] + (index + 1 < m ? dp[index + 1][left - 1][index][i] : 0) && (index + 1 == m || dp[index + 1][left - 1][index][i] != -1)) {
                            out.println((data[index][3] + 1) + " " + (data[index][0] + i));
                            left--;
                            last = index;
                            num = i;
                            break;
                        }
                    }
                } else {
                    long tmp = data[last][0] + num;
                    if (data[index][0] <= tmp + k && data[index][1] >= tmp + k) {
                        int nxt = (int) (tmp + k - data[index][0]);
                        if ((index + 1 == m || dp[index + 1][left - 1][index][nxt] != -1) && (index + 1 < m ? dp[index + 1][left - 1][index][nxt] : 0) + tmp + k == dp[index][left][last][num]) {
                            out.println((data[index][3] + 1) + " " + (tmp + k));
                            left--;
                            last = index;
                            num = nxt;
                            index++;
                            continue;
                        }
                    }
                    int nxt = (int) (tmp * k - data[index][0]);
                    out.println((data[index][3] + 1) + " " + (tmp * k));
                    left--;
                    last = index;
                    num = nxt;

                }
                index++;
            }
        } else {
            out.println("NO");
        }
        out.close();
    }

    public static long cal(int index, int left, int last, int num, int m, long k, long[][] data) {
        if (left == 0) {
            if (index < m) {
                dp[index][left][last][num] = 0;
            }
            return 0;
        }
        if (m - index < left) {
            return -1;
        }

        if (dp[index][left][last][num] != -1) {
            return dp[index][left][last][num];
        }
        long result = -1;

        if (last == m) {
            int max = (int) (data[index][1] - data[index][0]);
            boolean[] check = new boolean[max + 1];
            for (int i = max; i >= 0; i--) {
                if (!set.contains(data[index][0] + i)) {
                    set.add(data[index][0] + i);
                    check[i] = true;
                }
            }
            for (int i = max; i >= 0; i--) {
                if (!check[i]) {
                    continue;
                }
               // System.out.println((data[index][3] + 1) + "  " + (data[index][0] + i));

                long v = cal(index + 1, left - 1, index, i, m, k, data);
                if (v != -1) {
                    result = max(result, v + (long) i + data[index][0]);
                    if (k == 1) {
                        break;
                    }
                }
            }
        } else if (data[last][2] < data[index][2]) {
            long tmp = data[last][0] + num;
            if (data[index][0] <= tmp + k && data[index][1] >= tmp + k) {
                long v = cal(index + 1, left - 1, index, (int) (tmp + k - data[index][0]), m, k, data);
                if (v != -1) {
                    result = max(result, tmp + k + v);
                }
            }
            if (data[index][0] <= k * tmp && k * tmp <= data[index][1]) {
                long v = cal(index + 1, left - 1, index, (int) (tmp * k - data[index][0]), m, k, data);
                if (v != -1) {
                    result = max(result, tmp * k + v);
                }
            }
        }
        result = Long.max(result, cal(index + 1, left, last, num, m, k, data));
        return dp[index][left][last][num] = result;

    }

    static long max(long a, long b) {
        return a > b ? a : b;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return x - o.x;
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class C_Round_90 {

    public static long MOD = 1000000007;
    static long[][][][] dp;
    static HashSet<Long> set = new HashSet();

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        int m = in.nextInt();
        int k = in.nextInt();
        long[][] data = new long[m][4];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextLong();
            }
            data[i][3] = i;
        }
        Arrays.sort(data, new Comparator<long[]>() {
            @Override
            public int compare(long[] a, long[] b) {
                return Long.compare(a[2], b[2]);
            }

        }
        );

        dp = new long[m][n + 1][m + 1][101];
        for (long[][][] a : dp) {
            for (long[][] b : a) {
                for (long[] c : b) {
                    Arrays.fill(c, -1);
                }
            }
        }
        long v = cal(0, n, m, 0, m, k, data);
        // out.println(v);
        if(n == 39){
            System.out.println("HE HE");
        }
        if (v != -1) {
            out.println("YES");
            int index = 0;
            int left = n;
            int last = m;
            int num = 0;
            while (left > 0) {
                // System.out.println(index + " " + left + " " + last + " " + num + " " + (dp[index][left][last][num]));
                if (index + 1 < m && dp[index][left][last][num] == dp[index + 1][left][last][num]) {

                } else if (last == m) {
                    for (int i = 0; i + data[index][0] <= data[index][1]; i++) {
                        if (dp[index][left][last][num] == (long) i + data[index][0] + (index + 1 < m ? dp[index + 1][left - 1][index][i] : 0) && (index + 1 == m || dp[index + 1][left - 1][index][i] != -1)) {
                            out.println((data[index][3] + 1) + " " + (data[index][0] + i));
                            left--;
                            last = index;
                            num = i;
                            break;
                        }
                    }
                } else {
                    long tmp = data[last][0] + num;
                    if (data[index][0] <= tmp + k && data[index][1] >= tmp + k) {
                        int nxt = (int) (tmp + k - data[index][0]);
                        if ((index + 1 == m || dp[index + 1][left - 1][index][nxt] != -1) && (index + 1 < m ? dp[index + 1][left - 1][index][nxt] : 0) + tmp + k == dp[index][left][last][num]) {
                            out.println((data[index][3] + 1) + " " + (tmp + k));
                            left--;
                            last = index;
                            num = nxt;
                            index++;
                            continue;
                        }
                    }
                    int nxt = (int) (tmp * k - data[index][0]);
                    out.println((data[index][3] + 1) + " " + (tmp * k));
                    left--;
                    last = index;
                    num = nxt;

                }
                index++;
            }
        } else {
            out.println("NO");
        }
        out.close();
    }

    public static long cal(int index, int left, int last, int num, int m, long k, long[][] data) {
        if (left == 0) {
            if (index < m) {
                dp[index][left][last][num] = 0;
            }
            return 0;
        }
        if (m - index < left) {
            return -1;
        }

        if (dp[index][left][last][num] != -1) {
            return dp[index][left][last][num];
        }
        long result = -1;

        if (last == m) {
            int max = (int) (data[index][1] - data[index][0]);
            boolean[] check = new boolean[max + 1];
            for (int i = max; i >= 0; i--) {
                if (!set.contains(data[index][0] + i)) {
                    set.add(data[index][0] + i);
                    check[i] = true;
                }
            }
            for (int i = max; i >= 0; i--) {
                if (!check[i]) {
                    continue;
                }
               // System.out.println((data[index][3] + 1) + "  " + (data[index][0] + i));

                long v = cal(index + 1, left - 1, index, i, m, k, data);
                if (v != -1) {
                    result = max(result, v + (long) i + data[index][0]);
                    if (k == 1) {
                        break;
                    }
                }
            }
        } else if (data[last][2] < data[index][2]) {
            long tmp = data[last][0] + num;
            if (data[index][0] <= tmp + k && data[index][1] >= tmp + k) {
                long v = cal(index + 1, left - 1, index, (int) (tmp + k - data[index][0]), m, k, data);
                if (v != -1) {
                    result = max(result, tmp + k + v);
                }
            }
            if (data[index][0] <= k * tmp && k * tmp <= data[index][1]) {
                long v = cal(index + 1, left - 1, index, (int) (tmp * k - data[index][0]), m, k, data);
                if (v != -1) {
                    result = max(result, tmp * k + v);
                }
            }
        }
        result = Long.max(result, cal(index + 1, left, last, num, m, k, data));
        return dp[index][left][last][num] = result;

    }

    static long max(long a, long b) {
        return a > b ? a : b;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return x - o.x;
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.util.Scanner
public class Main{
public static void main(String[] args){
  Scanner sc=new Scanner();
  int arr={1,1,2,7,4};
  int min=Integer.Max_VALUE;
  for(int i=0;i<5;i++){
    int a=sc.nextInt();
    if(a/arr[i]<min){
      min=a/arr[i];
    }
  }
  System.out.println(min);
}
}


import java.util.Scanner
public class Main{
public static void main(String[] args){
  Scanner sc=new Scanner();
  int arr={1,1,2,7,4};
  int[] a=new int[5];
  int min=Integer.MAX_VALUE;
  for(int i=0;i<5;i++){
     a[i]=sc.nextInt();
    if(a[i]/arr[i]<min){
      min=a[i]/arr[i];
    }
  }
  System.out.println(min);
}
}


import java.util.*;
public class Main
{
    public static void main(String[]args)
    {
        int a[]={1,1,2,7,4};
        Scanner in=new Scanner(System.in);
        int b[]=new int[5];
        int min=MAX_VALUE;
        for(int i=0;i<5;i++)
        {
            b[i]=in.nextInt();
            if(b[i]/a[i]<min)
                min=b[i]/a[i];
        }
        System.out.println(min);
    }
}


import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

public class Taskc {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), p = p - p % 1000;
        ArrayList<Integer> a = new ArrayList<>();
        for (int i = p; i < n; i++) {
            if(i + sumDigits(i) == n) a.add(i);
        }
        if(a.size() == 0) System.out.println(0);
        else{
            System.out.println(a.size());
            for (Integer i : a) System.out.println(i);
        }
    }
    static int sumDigits(int n){
        int res= 0;
        while(n > 0){
            res += n % 10;
            n /= 10;
        }
        return res;
    }
    static int countOfDigits(int n){
        int res = 0;
        while (n > 0){
            n /= 10;
            res++;
        }
        return res;
    }
}


public class Main {

    public static void main(String[] args) {
        Scanner leer = new Scanner(System.in);
        while (leer.hasNext()) {
            int n=leer.nextInt();
            int cont=0;
            int argNum[]=new int[n];
            int arg[]=new int[0];
	    int sum=0;
            for (int i = 1; i < n-1; i++) {
                int k=i;
                arg=new int [(k+"").length()];
		        sum=0;
                for (int j = 0; j < arg.length; j++) {
                    arg[j]=Integer.parseInt((k+"").charAt(j)+"");
                    sum+=arg[j];
                }
                if(k+sum==n){
                    argNum[cont]=k;
                    cont++;
                }
            }
            System.out.println(cont);
            for (int i = 0; i < 10; i++) {
                if(argNum[i]!=0){
                    System.out.println(argNum[i]);
                }
            }
        }
    }

}


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

import static java.lang.Math.sqrt;
import static java.lang.Math.ceil;
import static java.lang.System.out;
import static java.lang.Math.pow;

public class 875A {
	private static final int N=(int) 2e5+5;
	private static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

	public static void main(String[] args) throws IOException{
		long b=Integer.parseInt(br.readLine());
		solve(b);
	}
	public static void solve(long b){
		ArrayList<Long> list=new ArrayList();
		int sum;
		for(long i=b;i>0;i--){
			if(i+81<b)break;
			else{
				sum=0;
				for(long j=i;j!=0;j/=10)sum+=j%10;
				if(sum+i==b){
					list.add(i);
				}
			}
		}
		out.println(list.size());
		Collections.sort(list);
		for(int i=0;i<list.size();i++)
			out.println(list.get(i));
	}
}


	public final class passed{
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int n,t=0;
		int[] L = new int[90];
		n = in.nextInt();
		for(int i=1;i<=81;i++){
			int x=n-i,sum=0;
			while(x>0){
				sum+=x%10;x/=10;
			}
			if(sum == i) L[t++]=n-i;
		}
		System.out.println(t);
		for(int i=t-1;i>=0;i--)
			System.out.println(L[i]);
	}
	}


 import java.util.*;
        import java.math.*;
        import java.io.*;
        import java.text.DecimalFormat;
        import java.math.BigInteger;
        public class Main{
            //static int d=20;
            static long mod=1000000007 ;
            static HashMap<String,Double> hm=new HashMap<>();
            static ArrayList<ArrayList<Integer>> arr;
            static long[] dp1,dp2;
            static int[] a;
            static int[] vis,num;
            static long max;
            static int id;
            public static void main(String[] args)  throws IOException {
                boolean online =false;
                String fileName = "C-large-practice";
                PrintWriter out;
                if (online) {
                    s.init(new FileInputStream(new File(fileName + ".in")));
                     out= new PrintWriter(new FileWriter(fileName + "out.txt"));
                }
                 else {
                    s.init(System.in);

                    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
                }//r,b,y,g
                long n=s.nl();
                String k=s.ns();
                long ans=0;
                long pow=0;
                long last=k.length()-1;
                long mul=1;
                for(int i=k.length()-1;i>=0;i--){
                    long x=k.charAt(i)-'0';
                    long u=x*mul*pow3(10,last-i);
                    long v=mul*n*x;
                    if(u<=v){
                        ans+=u;
                    }
                    else{
                        ans+=v;
                        mul*=n;
                        last=i;
                    }

                }
                out.println(ans);

                out.close();

            }
            public static long gcd(long a, long b){
                    if (a == 0) return b;
                    return gcd(b % a, a);
            }

            public static boolean check(long x){
                if(x%4==0 || x%7==0 || x%11==0)
                    return true;
                if(x<4 || x<7 || x<11)
                    return false;
                if(x%2==0){
                    return check(x/2);
                }
                else
                    return check(x/2) && check((x+1)/2);
            }
            public static int angle(int x1,int x2,int x3,int y1,int y2,int y3){
                long val=(y2-y1)*1L*(x3-x2)-(y3-y2)*1L*(x2-x1);
                if(val<0)
                    return 1;
                else if(val>0)
                    return -1;
                else
                    return 0;
            }

            static class BIT{

                int N;
                int tree[];

                BIT(int N){
                    this.N = N;
                    tree = new int[N + 1];
                }

                void update(int idx){
                    while(idx <= N){
                        tree[idx]++;
                        idx += (idx & -idx);
                    }
                }

                int query(int l,int r){
                    if(l == 1)
                        return read(r);
                    else
                        return read(r) - read(l-1);
                }

                int read(int idx){
                    int sum = 0;
                    while(idx > 0){
                        sum += tree[idx];
                        idx -= (idx & -idx);
                    }
                    return sum;
                }
            }
            public static boolean pal(String a){
                int i=0;
                int j=a.length()-1;
                while(i<j){
                    if(a.charAt(i)!=a.charAt(j)){
                        return false;
                    }
                    i++;
                    j--;
                }
                return true;

            }
            public static long find(long n,long k){
                if(n==2){
                    if(k==1 || k==3)
                        return 1;
                    else
                        return 2;
                }
                long t=pow2(2,n,mod)-1;
                long z=(t+1)/2;
                if(k==z)
                    return n;
                if(k<z)
                    return find(n-1,k);
                else
                    return find(n-1,k-z);
            }
            public static long pow2(long a,long b,long mod){
                    long ans=1;
                    while(b>0){
                        if(b%2==1)
                            ans=(a*ans)%mod;
                        a=(a*a)%mod;
                        b/=2;
                    }
                    return  ans;
            }
            public static double pow3(long a,long b){
                    long ans=1;
                    while(b>0){
                        if(b%2==1)
                            ans=(a*ans);
                        a=(a*a);
                        b/=2;
                    }
                    return  ans;
            }

             static class name implements Comparable<name> {
                long l,b,h;
                public name(long   x,long y,long z){
                    {
                        l=x;
                        b=y;
                    }

                    h=z;
                }
                public int compareTo(name o){
                    return -(int)(l*b-o.l*o.b);
                }

            }
            public static class s {
                static BufferedReader reader;
                static StringTokenizer tokenizer;

                /** call this method to initialize reader for InputStream */
                static void init(InputStream input) {
                    reader = new BufferedReader(
                                new InputStreamReader(input) );
                    tokenizer = new StringTokenizer("");
                }

                /** get next word */
                static String ns() throws IOException {
                    while ( ! tokenizer.hasMoreTokens() ) {
                        //TODO add check for eof if necessary
                        tokenizer = new StringTokenizer(
                            reader.readLine() );
                    }
                    return tokenizer.nextToken();
                }

                static int ni() throws IOException {
                    return Integer.parseInt( ns() );
                }

                static double nd() throws IOException {
                    return Double.parseDouble( ns() );
                }
                static long nl() throws IOException {
                    return Long.parseLong( ns() );
                }
            }
        }


import com.sun.deploy.util.ArrayUtil;

import java.math.BigInteger;
import java.util.*;
import java.io.*;

public class D {
    BufferedReader br;
    PrintWriter out;
    StringTokenizer stok;

    String nextToken() throws IOException {
        while (stok == null || !stok.hasMoreTokens()) {
            String s = br.readLine();
            if (s == null) {
                return null;
            }
            stok = new StringTokenizer(s);
        }
        return stok.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    char nextChar() throws IOException {
        return (char) (br.read());
    }

    String nextLine() throws IOException {
        return br.readLine();
    }

    private void solve() throws IOException {
        int n = nextInt();
        String s = nextToken();
        if (n <= 10)
        {
            BigInteger bigInteger = new BigInteger(s, n);
            out.println(bigInteger.toString());
            return;
        }

        ArrayList<Integer> integers = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        char[] c = s.toCharArray();

        int i = s.length() - 1;
        while (i >= 0)
        {
            int j;
            for (j = i - 1; j >= 0; j--) {
                int x = Integer.valueOf(s.substring(j, i + 1));
                if (x >= n) {
                    do {
                        j++;
                    } while (j < i && s.charAt(j) == '0');
                    break;
                }
            }
            j = Math.max(j, 0);
            integers.add(Integer.valueOf(s.substring(j, i + 1)));
            i = j - 1;
        }

        long ans = 0;
        for (int q = integers.size() - 1; q >= 0; q--) {
            ans = ans * n + integers.get(q);
//            System.err.println(integers.get(q));
        }
        out.println(ans);
    }

    void run() throws IOException {
        // br = new BufferedReader(new FileReader("input.txt"));
        // out = new PrintWriter("output.txt");
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        br.close();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        // Locale.setDefault(Locale.US);
        new D().run();
    }
}