# include <bits/stdc++.h>

# define y1 aklsdjskldjls

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;

const int N = int(4e5) + 5;
const int inf = (int)1e9 + 7;
const ll linf = (ll)1e17 + 7;
const ld eps = 1e-7;

ll ond = 1, e, q, k, zero;
string s;
char check(char x){
  if(x != 'a' && x != 'e' && x != 'i' && x != 'o' && x != 'u')
    return 1;
  return 0;
}
int main(){
  cin >> s;
  cout << s[0];
  if(check(s[0]))
    q = 1;
  for(ll i = 1; i < s.size(); i++){
    if(check(s[i])){
      q++;
      if(check(s[i - 1]))
        if(s[i - 1] != s[i])
          e = 1;
    }
    else
      q = 0, e = 0;
   // cout << q << " " << e << "\n";
    if(q >= 3 && e == 1)
      q = 1, cout << " ";
    cout << s[i];
    if(s[i - 1] == s[i])
      e = 0;
  }
  return 0;
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Sagor
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ReplacingDigits solver = new ReplacingDigits();
        solver.solve(1, in, out);
        out.close();
    }

    static class ReplacingDigits {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            String s = in.nextLine();
            String p = in.nextLine();
            char pp[] = p.toCharArray();
            char ss[] = s.toCharArray();
            Arrays.sort(pp);
            int len = s.length();
            int ppl = p.length();
            for (int i = 0; i < len; i++) {
                for (int j = ppl - 1; j >= 0; j--) {
                    if (pp[j] > ss[i]) {
                        ss[i] = pp[j];
                        pp[j] = '0';
                        break;
                    }
                }
            }
            for (int i = 0; i < len; i++) {
                out.print(ss[i]);
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Sagor
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ReplacingDigits solver = new ReplacingDigits();
        solver.solve(1, in, out);
        out.close();
    }

    static class ReplacingDigits {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            String s = in.nextLine();
            String p = in.nextLine();
            char pp[] = p.toCharArray();
            char ss[] = s.toCharArray();
            Arrays.sort(pp);
            int len = s.length();
            int ppl = p.length();
            for (int i = 0; i < len; i++) {
                if (ss[i] == '9') {
                    continue;
                }
                for (int j = ppl - 1; j >= 0; j--) {
                    if (pp[j] == '0') continue;
                    if (pp[j] > ss[i]) {
                        ss[i] = pp[j];
                        pp[j] = '0';
                    }
                    break;
                }
            }
            for (int i = 0; i < len; i++) {
                out.print(ss[i]);
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Sagor
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ReplacingDigits solver = new ReplacingDigits();
        solver.solve(1, in, out);
        out.close();
    }

    static class ReplacingDigits {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            String s = in.nextLine();
            String p = in.nextLine();
            char pp[] = p.toCharArray();
            char ss[] = s.toCharArray();
            Arrays.sort(pp);
            int len = s.length();
            int ppl = p.length();
            for (int i = 0; i < len; i++) {
                if (ss[i] == '9') {
                    continue;
                }
                if (ppl < 1)
                    break;
                if (pp[ppl - 1] > ss[i]) {
                    ss[i] = pp[ppl - 1];
                    ppl--;
                }
            }
            for (int i = 0; i < len; i++) {
                out.print(ss[i]);
            }
        }

    }
}


import java.util.Scanner;
import java.util.Arrays;
import java.lang.Math;

public class A {
	public static void main(String[] args) {
		Scanner s = new Scanner (System.in);
		String n = s.next();
		String digit = s.next();
		int l = n.length();
		char[] result = new char [l];
		int length = digit.length();
		int index = length-1;
		char[] a = new char [length];
		for (int i=0;i<length;i++)
		{
			a[i] = digit.charAt(i);
		}
		Arrays.sort(a);
		for (int i=0;i<l;i++)
		{
			if (index>=0 && a[index]>n.charAt(i))
			{
				result[i] = a[index];
				index--;
			}
			else
			{
				result[i] = n.charAt(i);
			}
		}
		String res = new String(result);
		System.out.print(res);
		/*for (int i=0;i<n;i++)
		{
			System.out.print(array[i]+" ");
		}*/
		s.close();
	}

}


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

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String one =sc.nextLine();
        String two = sc.nextLine();

        ArrayList<Character> charOne = new ArrayList<Character>();
        for (char c : one.toCharArray()) {
            charOne.add(c);
        }
        ArrayList<Character> charTwo = new ArrayList<Character>();
        for (char c : two.toCharArray()) {
            charTwo.add(c);
        }
        ArrayList<Character> output = new ArrayList<Character>();
        if(charOne.size()<=6){//why is there a test with 99999999999999999999999999999
            for(int i=0;i<charOne.size();i++) {
                if (charTwo.size() == 0) {
                    output.add(charOne.get(i));
                    continue;
                }
                for (int j = 0; j < charTwo.size(); j++) {
                    if (charOne.get(i) >= charTwo.get(j)) {
                        if (j == charTwo.size() - 1) {
                            output.add(charOne.get(i));
                            break;
                        }
                        continue;
                    } else {
                        output.add(charTwo.remove(j));
                        j--;
                        break;
                    }
                }
            }
            for(Character c:output) {
                System.out.print(c);
            }
        }
        sc.close();

    }

}


package com.company;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String one =sc.nextLine();
        String two = sc.nextLine();

        ArrayList<Character> charOne = new ArrayList<Character>();
        for (char c : one.toCharArray()) {
            charOne.add(c);
        }
        ArrayList<Character> charTwo = new ArrayList<Character>();
        for (char c : two.toCharArray()) {
            charTwo.add(c);
        }

        ArrayList<Character> output = new ArrayList<Character>();
        for(int i=0;i<charOne.size();i++){
            for(int j=0;j<charTwo.size();j++){
                if(charOne.get(i)>=charTwo.get(j)){
                    output.add(charOne.get(i));
                }else{
                    output.add(charTwo.remove(j));
                    j--;
                }
            }
        }

        for(char c:output) {
            System.out.print(c);
        }

    }

}


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

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String one =sc.next();
        String two = sc.next();

        char[] charOne = one.toCharArray();
        char[] charTwo = two.toCharArray();

        Arrays.sort(charTwo);//sort ascending
        int p = charTwo.length-1;
        for(int i=0;i<charOne.length;i++){
            if(p>= 0 && charTwo[p] > charOne[i]){
                charOne[i] = charTwo[p];
                p--;
            }
        }

        System.out.print(charOne);

        sc.close();
    }

}


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

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String one =sc.nextLine();
        String two = sc.nextLine();

        ArrayList<Character> charOne = new ArrayList<Character>();
        for (char c : one.toCharArray()) {
            charOne.add(c);
        }
        ArrayList<Character> charTwo = new ArrayList<Character>();
        for (char c : two.toCharArray()) {
            charTwo.add(c);
        }
        if(charOne.size()<=6){//why is there a test with 99999999999999999999999999999
        ArrayList<Character> output = new ArrayList<Character>();
            for(int i=0;i<charOne.size();i++) {
                if (charTwo.size() == 0) {
                    output.add(charOne.get(i));
                    continue;
                }
                for (int j = 0; j < charTwo.size(); j++) {
                    if (charOne.get(i) >= charTwo.get(j)) {
                        if (j == charTwo.size() - 1) {
                            output.add(charOne.get(i));
                            break;
                        }
                        continue;
                    } else {
                        output.add(charTwo.remove(j));
                        j--;
                        break;
                    }
                }
            }
        }

        for(Character c:output) {
            System.out.print(c);
        }

    }

}


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

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String one =sc.nextLine();
        String two = sc.nextLine();

        ArrayList<Character> charOne = new ArrayList<Character>();
        for (char c : one.toCharArray()) {
            charOne.add(c);
        }
        ArrayList<Character> charTwo = new ArrayList<Character>();
        for (char c : two.toCharArray()) {
            charTwo.add(c);
        }

        ArrayList<Character> output = new ArrayList<Character>();
        for(int i=0;i<charOne.size();i++){
            for(int j=0;j<charTwo.size();j++){
                if(charOne.get(i)>=charTwo.get(j)){
                    if(j==charTwo.size()-1){
                        output.add(charOne.get(i));
                        break;
                    }
                    continue;
                }else{
                    output.add(charTwo.remove(j));
                    j--;
                    break;
                }
            }
        }

        for(Character c:output) {
            System.out.print(c);
        }

    }

}


package com.company;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String one =sc.nextLine();
        String two = sc.nextLine();

        ArrayList<Character> charOne = new ArrayList<Character>();
        for (char c : one.toCharArray()) {
            charOne.add(c);
        }
        ArrayList<Character> charTwo = new ArrayList<Character>();
        for (char c : two.toCharArray()) {
            charTwo.add(c);
        }

        ArrayList<Character> output = new ArrayList<Character>();
        for(int i=0;i<charOne.size();i++){
            for(int j=0;j<charTwo.size();j++){
                if(charOne.get(i)>=charTwo.get(j)){
                    if(j==charTwo.size()-1){
                        output.add(charOne.get(i));
                        break;
                    }
                    continue;
                }else{
                    output.add(charTwo.remove(j));
                    j--;
                    break;
                }
            }
        }

        for(char c:output) {
            System.out.print(c);
        }

    }

}


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

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String one =sc.nextLine();
        String two = sc.nextLine();

        ArrayList<Character> charOne = new ArrayList<Character>();
        for (char c : one.toCharArray()) {
            charOne.add(c);
        }
        ArrayList<Character> charTwo = new ArrayList<Character>();
        for (char c : two.toCharArray()) {
            charTwo.add(c);
        }

        ArrayList<Character> output = new ArrayList<Character>();
        for(int i=0;i<charOne.size();i++){
            if(charTwo.size()==0){
                output.add(charOne.get(i));
                continue;
            }
            for(int j=0;j<charTwo.size();j++){
                if(charOne.get(i)>=charTwo.get(j)){
                    if(j==charTwo.size()-1){
                        output.add(charOne.get(i));
                        break;
                    }
                    continue;
                }else{
                    output.add(charTwo.remove(j));
                    j--;
                    break;
                }
            }
        }

        for(Character c:output) {
            System.out.print(c);
        }

    }

}


package com.company;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String one =sc.nextLine();
        String two = sc.nextLine();

        ArrayList<Character> charOne = new ArrayList<Character>();
        for (char c : one.toCharArray()) {
            charOne.add(c);
        }
        ArrayList<Character> charTwo = new ArrayList<Character>();
        for (char c : two.toCharArray()) {
            charTwo.add(c);
        }

        ArrayList<Character> output = new ArrayList<Character>();
        for(int i=0;i<charOne.size();i++){
            for(int j=0;j<charTwo.size();j++){
                if(charOne.get(i)>=charTwo.get(j)){
                    if(j==charTwo.size()-1){
                        output.add(charOne.get(i));
                        break;
                    }
                    continue;
                }else{
                    output.add(charTwo.remove(j));
                    j--;
                    break;
                }
            }
        }

        for(Character c:output) {
            System.out.print(c);
        }

    }

}


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

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String one =sc.nextLine();
        String two = sc.nextLine();

        ArrayList<Character> charOne = new ArrayList<Character>();
        for (char c : one.toCharArray()) {
            charOne.add(c);
        }
        ArrayList<Character> charTwo = new ArrayList<Character>();
        for (char c : two.toCharArray()) {
            charTwo.add(c);
        }
        ArrayList<Character> output = new ArrayList<Character>();
        if(charOne.size()<=6){//why is there a test with 99999999999999999999999999999
            for(int i=0;i<charOne.size();i++) {
                if (charTwo.size() == 0) {
                    output.add(charOne.get(i));
                    continue;
                }
                for (int j = 0; j < charTwo.size(); j++) {
                    if (charOne.get(i) >= charTwo.get(j)) {
                        if (j == charTwo.size() - 1) {
                            output.add(charOne.get(i));
                            break;
                        }
                        continue;
                    } else {
                        output.add(charTwo.remove(j));
                        j--;
                        break;
                    }
                }
            }
        }

        for(Character c:output) {
            System.out.print(c);
        }

    }

}


#define _CRT_SECURE_NO_WARNINGS
#include <cmath>

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long double ld;
typedef vector<int> vi;
typedef long long ll;

string s;
string t;

void input() {
  cin >> s >> t;
}

void solve() {
  for (int i = 0; i < (int)min(s.size(), t.size()); ++i) {
    if (s[i] < t[i])
      s[i] = t[i];
  }

  cout << s << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cout.precision(16);
  cout << fixed;

  int _times = 1;

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);

  cin >> _times;
#endif

  while (_times--) {
    input();
    solve();
  }
}


#define _CRT_SECURE_NO_WARNINGS
#include <cmath>

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#include <set>

using namespace std;

typedef long double ld;
typedef vector<int> vi;
typedef long long ll;

string s;

multiset<int, greater<int>> a;

void input() {
  cin >> s;

  string t;
  cin >> t;

  for (auto&& i : t)
    a.insert(i - '0');
}

void solve() {
  for (int i = 0; i < (int)s.size() && !a.empty(); ++i) {
    if (s[i] - '0' < *a.cbegin()) {
      s[i] = *a.cbegin() + '0';

      a.erase(a.cbegin());
    }
  }

  cout << s << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cout.precision(16);
  cout << fixed;

  int _times = 1;

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);

  cin >> _times;
#endif

  while (_times--) {
    input();
    solve();
  }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    string n,m;cin>>n>>m;
    sort(m.begin(), m.end());
    for(int i=m.size()-1;i>=0;i--)
    {
        for(int j=0;j<n.size();j++)
        {
           if(m[i]>n[j]){
            n[j]=m[i];
            m[i]='0';
            break;
           }
        }
    }
    cout<<n;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    string n,m;cin>>n>>m;
    sort(m.begin(), m.end());
    int k=m.size()-1;
    for(int i=0;i<n.size();i++)
    {
        if(m[k]>n[i]){
            n[i]=m[k];
            k--;
        }
    }
    cout<<n;
}


#include<bits/stdc++.h>
using namespace std;
#define  fast()      ios :: sync_with_stdio(false); cin.tie(0);
#define  ll             long long int
#define  ull            unsigned long long int
#define     sz(x)       (int)x.size()
#define  ld             long double
#define  vi             vector<int>
#define vl              vector<long long>
#define vc              vector<char>
#define vs              vector<string>
#define vp              vector< pair<ll,ll> >
#define sl              set<long long>
#define sc              set<char>
#define sts             stack<string>
#define max_3(a,b,c)    max(a, max(b,c))
#define max_4(a,b,c,d)  max(a,max(b,max(c,d)))
#define min_3(a,b,c)    min(a, min(b,c))
#define min_4(a,b,c,d)  min(a,min(b,min(c,d)))
#define gcd(a,b)        __gcd(a,b)
#define lcm(a,b)        (a*b)/gcd(a,b)
#define pi              2*acos(0.0)
#define N               100006
#define in              1234567890;
int main()
{
    fast()
    /*freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);*/
    ll n,i,t,j=0,x,y,k;
    string s,p;
    char c='0';
    cin>>s>>p;
    for(i=0;i<p.size();i++) c=max(c,p[i]);
    for(i=0;i<s.size();i++) if(c>s[i]&&j==0) s[i]=c,j++;
    cout<<s;

}


#include<bits/stdc++.h>
using namespace std;
#define  fast()      ios :: sync_with_stdio(false); cin.tie(0);
#define  ll             long long int
#define  ull            unsigned long long int
#define     sz(x)       (int)x.size()
#define  ld             long double
#define  vi             vector<int>
#define vl              vector<long long>
#define vc              vector<char>
#define vs              vector<string>
#define vp              vector< pair<ll,ll> >
#define sl              set<long long>
#define sc              set<char>
#define sts             stack<string>
#define max_3(a,b,c)    max(a, max(b,c))
#define max_4(a,b,c,d)  max(a,max(b,max(c,d)))
#define min_3(a,b,c)    min(a, min(b,c))
#define min_4(a,b,c,d)  min(a,min(b,min(c,d)))
#define gcd(a,b)        __gcd(a,b)
#define lcm(a,b)        (a*b)/gcd(a,b)
#define pi              2*acos(0.0)
#define N               100006
#define in              1234567890;
int main()
{
    fast()
    /*freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);*/
    ll n,i,t,j=0,x,y,k;
    string s,p;
    char c='0';
    cin>>s>>p;
    k=p.size();
    for(i=0;i<p.size();i++) c=max(c,p[i]);
    for(i=0;i<s.size();i++) if(c>s[i]&&k>0) s[i]=c,k--;
    cout<<s;

}


#include<bits/stdc++.h>
using namespace std;
#define  fast()      ios :: sync_with_stdio(false); cin.tie(0);
#define  ll             long long int
#define  ull            unsigned long long int
#define     sz(x)       (int)x.size()
#define  ld             long double
#define  vi             vector<int>
#define vl              vector<long long>
#define vc              vector<char>
#define vs              vector<string>
#define vp              vector< pair<ll,ll> >
#define sl              set<long long>
#define sc              set<char>
#define sts             stack<string>
#define max_3(a,b,c)    max(a, max(b,c))
#define max_4(a,b,c,d)  max(a,max(b,max(c,d)))
#define min_3(a,b,c)    min(a, min(b,c))
#define min_4(a,b,c,d)  min(a,min(b,min(c,d)))
#define gcd(a,b)        __gcd(a,b)
#define lcm(a,b)        (a*b)/gcd(a,b)
#define pi              2*acos(0.0)
#define N               100006
#define in              1234567890;
int main()
{
    fast()
    /*freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);*/
    string s,p;
    cin>>p>>s;
    sort(s.begin(),s.end());

	int i=0,j=s.size()-1;

	while(i<p.size() && j>=0)
	{
		if(p[i]<s[j])
		{
			p[i]=s[j];
			i++;
			j--;
		}
		else
		{
			i++;
		}
	}
	cout<<p<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
#define VALS(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define pf push_front
#define INF 1234567890ll
#define PI (acos((ld)-1.0))
#define ll long long int
#define ld long double
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
typedef pair <int, int> pi;
typedef pair <ll, ll> pl;
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;

int main()
{
	IAMSPEED
    string o, t; cin >> o >> t;
    FOR(i, 0, o.size()-1) {
		int x = max(o[i] - '0', t[i] - '0');
        o[i] = (char)(x + '0');
    }
    cout << o;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
#define VALS(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define pf push_front
#define INF 1234567890ll
#define PI (acos((ld)-1.0))
#define ll long long int
#define ld long double
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
typedef pair <int, int> pi;
typedef pair <ll, ll> pl;
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;

bool cmp(char a, char b) {
	return a - '0' < b - '0';
}

int main()
{
	IAMSPEED
	string a, b; cin >> a >> b;
	sort(b.begin(), b.end(), cmp);
	FOR(i, 0, a.size()-1) {
		if (!b.empty() && b.back() - '0' > a[i] - '0') {
			a[i] = b.back();
			b.pop_back();
		}
	}
	cout << a;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a,s;
    cin>>a>>s;
    int len1=a.size();
    int len2=s.size();
    int len=max(len1,len2);
    sort(s.begin(),s.end());
    reverse(s.begin(),s.end());
    int j=0;
    for(int i=0;i<len1;i++)
    {
        if(j<len2&&a[i]<s[j])
            a[i]=s[j++];
    }
    cout<<a<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a,s;
    cin>>a>>s;
    int len1=a.size();
    int len2=s.size();
    int len=max(len1,len2);

    for(int i=0;i<len;i++)
    {
        a[i]=max(a[i],s[i]);
    }
    cout<<a<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef vector<ll> vi;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll a,s;
    cin>>a>>s;
    vi a1;
    vi s1;
    while(a!=0){
        ll p=a%10;
        a=a/10;
        a1.push_back(p);
    }
    while(s!=0){
        ll p=s%10;
        s=s/10;
        s1.push_back(p);
    }
    sort(s1.begin(),s1.end());
    ll j=s1.size()-1;
    for(ll i=a1.size()-1;i>=0;i--){
        if(a1[i]>=s1[j])
            continue;
        else{
            a1[i]=s1[j];
            j--;
        }
    }
    for(ll j=a1.size()-1;j>=0;j--)
        cout<<a1[j];
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef vector<ll> vi;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll a,s;
    cin>>a>>s;
    vi a1;
    vi s1;
    if(s==0){
        cout<<a;
        return 0;}
    while(a!=0){
        ll p=a%10;
        a=a/10;
        a1.push_back(p);
    }
    while(s!=0){
        ll p=s%10;
        s=s/10;
        s1.push_back(p);
    }
    if(a==0){
        cout<<*max_element(s1.begin(), s1.end());
        return 0;
    }
    sort(s1.begin(),s1.end());
    ll j=s1.size()-1;
    for(ll i=a1.size()-1;i>=0;i--){
        if(a1[i]>=s1[j])
            continue;
        else{
            if(j>=0){
            a1[i]=s1[j];
            j--;}
        }
    }
    for(ll j=a1.size()-1;j>=0;j--)
        cout<<a1[j];
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef vector<ll> vi;
int main(){
    ios_base::sync_with_stdio(false);
    string a,s;
    cin >> a >> s;
	sort(s.rbegin(),s.rend());
	int x = a.size();
	int y = s.size();
	int j=0;
	for(int i=0;i<x;i++){
	    if(j<y){
		if(s[j]>a[i])
			a[i]=s[j++];}
	}
	cout << a;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef vector<ll> vi;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll a,s;
    cin>>a>>s;
    vi a1;
    vi s1;
    while(a!=0){
        ll p=a%10;
        a=a/10;
        a1.push_back(p);
    }
    while(s!=0){
        ll p=s%10;
        s=s/10;
        s1.push_back(p);
    }
    sort(s1.begin(),s1.end());
    ll j=s1.size()-1;
    for(ll i=a1.size()-1;i>=0;i--){
        if(a1[i]>=s1[j])
            continue;
        else{
            if(j>=0){
            a1[i]=s1[j];
            j--;}
        }
    }
    for(ll j=a1.size()-1;j>=0;j--)
        cout<<a1[j];
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef vector<ll> vi;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll a,s;
    cin>>a>>s;
    vi a1;
    vi s1;
    if(s==0){
        cout<<a;
        return 0;}
    while(s!=0){
        ll p=s%10;
        s=s/10;
        s1.push_back(p);
    }
    if(a==0){
        cout<<*max_element(s1.begin(), s1.end());
        return 0;
    }
    while(a!=0){
        ll p=a%10;
        a=a/10;
        a1.push_back(p);
    }
    sort(s1.begin(),s1.end());
    ll j=s1.size()-1;
    for(ll i=a1.size()-1;i>=0;i--){
        if(a1[i]>=s1[j])
            continue;
        else{
            if(j>=0){
            a1[i]=s1[j];
            j--;}
        }
    }
    for(ll j=a1.size()-1;j>=0;j--)
        cout<<a1[j];
    return 0;
}


#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;

using ll = long long;
constexpr ll mod = 1e9 + 7;
#define endl '\n'

template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

struct Scanner {
  int fd = -1;
  char line[(1 << 15) + 1];
  size_t st = 0, ed = 0;
  void reread() {
    memmove(line, line + st, ed - st);
    ed -= st;
    st = 0;
    ed += ::read(fd, line + ed, (1 << 15) - ed);
    line[ed] = '\0';
  }
  bool succ() {
    while (true) {
      if (st == ed) {
        reread();
        if (st == ed) return false;
      }
      while (st != ed && isspace(line[st])) st++;
      if (st != ed) break;
    }
    if (ed - st <= 50) {
      bool sep = false;
      for (size_t i = st; i < ed; i++) {
        if (isspace(line[i])) {
          sep = true;
          break;
        }
      }
      if (!sep) reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
  bool read_single(T& ref) {
    if (!succ()) return false;
    while (true) {
      size_t sz = 0;
      while (st + sz < ed && !isspace(line[st + sz])) sz++;
      ref.append(line + st, sz);
      st += sz;
      if (!sz || st != ed) break;
      reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  bool read_single(T& ref) {
    if (!succ()) return false;
    bool neg = false;
    if (line[st] == '-') {
      neg = true;
      st++;
    }
    ref = T(0);
    while (isdigit(line[st])) {
      ref = 10 * ref + (line[st++] & 0xf);
    }
    if (neg) ref = -ref;
    return true;
  }
  template <class T> bool read_single(V<T>& ref) {
    for (auto& d : ref) {
      if (!read_single(d)) return false;
    }
    return true;
  }
  template <class T> bool read_single(array<T,2>& ref) {
    if (!read_single(ref[0])) return false;
    if (!read_single(ref[1])) return false;
    return true;
  }
  template <class T> bool read_single(array<T,3>& ref) {
    if (!read_single(ref[0])) return false;
    if (!read_single(ref[1])) return false;
    if (!read_single(ref[2])) return false;
    return true;
  }
  template <class T> bool read_single(array<T,4>& ref) {
    if (!read_single(ref[0])) return false;
    if (!read_single(ref[1])) return false;
    if (!read_single(ref[2])) return false;
    if (!read_single(ref[3])) return false;
    return true;
  }
  void read() {}
  template <class H, class... T> void read(H& h, T&... t) {
    bool f = read_single(h);
    assert(f);
    read(t...);
  }
  Scanner(FILE* fp) : fd(fileno(fp)) {}
};

struct Printer {
public:
  template <bool F = false> void write() {}
  template <bool F = false, class H, class... T>
  void write(const H& h, const T&... t) {
    if (F) write_single(' ');
    write_single(h);
    write<true>(t...);
  }
  template <class... T> void writeln(const T&... t) {
    write(t...);
    write_single('\n');
  }

  Printer(FILE* _fp) : fp(_fp) {}
  ~Printer() { flush(); }

private:
  static constexpr size_t SIZE = 1 << 15;
  FILE* fp;
  char line[SIZE], small[50];
  size_t pos = 0;
  void flush() {
    fwrite(line, 1, pos, fp);
    pos = 0;
  }
  void write_single(const char& val) {
    if (pos == SIZE) flush();
    line[pos++] = val;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  void write_single(T val) {
    if (pos > (1 << 15) - 50) flush();
    if (val == 0) {
      write_single('0');
      return;
    }
    if (val < 0) {
      write_single('-');
      val = -val; // todo min
    }
    size_t len = 0;
    while (val) {
      small[len++] = char(0x30 | (val % 10));
      val /= 10;
    }
    for (size_t i = 0; i < len; i++) {
      line[pos + i] = small[len - 1 - i];
    }
    pos += len;
  }
  void write_single(const string& s) {
    for (char c : s) write_single(c);
  }
  void write_single(const char* s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++) write_single(s[i]);
  }
  template <class T> void write_single(const V<T>& val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
      if (i) write_single(' ');
      write_single(val[i]);
    }
  }
};

Scanner sc = Scanner(stdin);
Printer pr = Printer(stdout);
#define int ll

int32_t main() {
  ios::sync_with_stdio(0); cin.tie(0);

  string s1,s2;
  sc.read(s1,s2);
  sort(s2.rbegin(), s2.rend());
  int i2 = 0;
  for (int i = 0; i < s1.length() && i2 < s2.length(); ++i) {
    if (s2[i2] > s1[i]) {
      s1[i] = s2[i2++];
    }
  }
  pr.writeln(s1);

}


#include<bits/stdc++.h>
using namespace std;
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main()
{
  optimize();
  string s,n;
  int i,j,l;
  cin>>s>>n;

    sort(n.rbegin(),n.rend());
    j=0;
    for(i=0;i<s.size();i++)
    {
        if(s[i]<n[j]){
            s[i]=n[j];
        j++;}
    }
    cout<<s<<endl;

    return 0;
}


// IN THE NAME OF ALLAH
// MOST GRACIOUS, MOST MERCIFUL
// We mere live in a simulation, a game
// and it's programmer is one and only Allah!
#include <bits/stdc++.h>
#include <iomanip>
#define N 123456789
#define INF INFINITY
#define ll long long
using namespace std;

//sieve of eratosthenes
vector<int>prime;
void SieveOfEratosthenes(int n){
	bool prme[n+1];
    memset(prme, true, sizeof(prme));
    for (int p=2; p*p<=n; p++){
        if (prme[p] == true){
            for (int i=p*p; i<=n; i += p)
                prme[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prme[p])
          prime.push_back(p);
}

void solve()
{
	string a,s;cin>>a>>s;
	sort(s.rbegin(), s.rend());
	int len = (int)s.length();
	for(int i = 0;i<(int)a.length();i++){
		if(len <= 0)break;
		for(int j = 0;j<(int)s.length();j++){
			if(s[j]>a[i] && s[j]!='X'){
				a[i] = s[j];
				s[j] = 'X';
				len--;
			}else if(s[j]<a[i] && s[j]!='X'){
				break;
			}
		}
	}
	cout<<a;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	/*int t;cin>>t;
	while(t--)solve();
	*/solve();
}


// IN THE NAME OF ALLAH
// MOST GRACIOUS, MOST MERCIFUL
// We mere live in a simulation, a game
// and it's programmer is one and only Allah!
#include <bits/stdc++.h>
#include <iomanip>
#define N 123456789
#define INF INFINITY
#define ll long long
using namespace std;

//sieve of eratosthenes
vector<int>prime;
void SieveOfEratosthenes(int n){
	bool prme[n+1];
    memset(prme, true, sizeof(prme));
    for (int p=2; p*p<=n; p++){
        if (prme[p] == true){
            for (int i=p*p; i<=n; i += p)
                prme[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prme[p])
          prime.push_back(p);
}

void solve()
{
	string a,s;cin>>a>>s;
	sort(s.rbegin(), s.rend());
	for(int i = 0;i<(int)a.length();i++){
		for(int j = 0;j<(int)s.length();j++){
			if(s[j]>a[i] && s[j]!='X'){
				a[i] = s[j];
				s[j] = 'X';
			}else if(s[j]<a[i] && s[j]!='X'){
				break;
			}
		}
	}
	cout<<a;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	/*int t;cin>>t;
	while(t--)solve();
	*/solve();
}


// IN THE NAME OF ALLAH
// MOST GRACIOUS, MOST MERCIFUL
// We mere live in a simulation, a game
// and it's programmer is one and only Allah!
#include <bits/stdc++.h>
#include <iomanip>
#define N 123456789
#define INF INFINITY
#define ll long long
using namespace std;

//sieve of eratosthenes
vector<int>prime;
void SieveOfEratosthenes(int n){
	bool prme[n+1];
    memset(prme, true, sizeof(prme));
    for (int p=2; p*p<=n; p++){
        if (prme[p] == true){
            for (int i=p*p; i<=n; i += p)
                prme[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prme[p])
          prime.push_back(p);
}

void solve()
{
	string a,s;cin>>a>>s;
	sort(s.rbegin(), s.rend());
	int j = 0;
	for(int i = 0;i<(int)a.length();i++){
		if(j>=(int)s.length())break;
		else{
			if(a[i] < s[j]){
				a[i] = s[j];
				j++;
			}
		}
	}
	cout<<a;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	/*int t;cin>>t;
	while(t--)solve();
	*/solve();
}


#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;

// 0 < a
// 0 < n < 10^5
// Find max a' replaced using s

int main() {
  string a;
  string s;
  cin >> a >> s;

  vector<int> na((int) a.size());
  vector<int> ns((int) s.size());
  for (int i = 0 ; i < (int) a.size() ; ++i) {
    na[i] = (int) a[i] - '0';
  }
  for (int i = 0 ; i < (int) s.size() ; ++i) {
    ns[i] = (int) s[i] - '0';
  }

  // sort s in decreasing order
  sort(ns.rbegin(), ns.rend());

  int a_len = (int) na.size();
  int s_len = (int) ns.size();

  for (int i = 0, si = 0 ; i < a_len && si < s_len ; ++i) {
    if (na[i] >= ns[si]) continue;

    na[i] = ns[si];
    si++;
  }

  for (auto d : na) cout << d;
  cout << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,m;
    cin>>s>>m;
    for(long long int i=0;i<s.length();i++)
    {sort(m.begin(),m.end());
        if(s[i]<m[m.length()-1])
        {
            s[i]=m[m.length()-1];
            m[m.length()-1]=0;
        }
    }
    cout<<s;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    ll x,y;
    cin>>x>>y;
    vector<ll>a;
    vector<ll>b;
    while(1)
    {if(x>0)
        {a.push_back(x%10);
        x=x/10;
        }
        if(y>0)
        {
            b.push_back(y%10);
            y=y/10;
        }
        if(x<=0&&y<=0)
        break;
    }
    ll i=(signed long long int)a.size()-1;
    while(i>=0)
    {   if(b.size()>0&&a.size()>0)
           {
                sort(b.begin(),b.end());
        if(a[i]<b[b.size()-1])
        {
            a[i]=b[b.size()-1];
            i--;
            b[b.size()-1]=0;
        }
        else
        i--;
           }
           else
            break;
    }
    for(ll i=(signed long long int)a.size()-1;i>=0;i--)
        cout<<a[i];
    }


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,m;
    cin>>s>>m;
    sort(m.begin(),m.end());
    long long int j=m.length()-1;
    for(long long int i=0;i<s.length();i++)
    {
        if(s[i]<m[j])
        {
            s[i]=m[j];
            m[j]=0;
            j--;
        }
        if(m[j]==0)
            break;
    }
    cout<<s;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    ll x,y;
    cin>>x>>y;
    vector<ll>a;
    vector<ll>b;
    while(1)
    {if(x>0)
        {a.push_back(x%10);
        x=x/10;
        }
        if(y>0)
        {
            b.push_back(y%10);
            y=y/10;
        }
        if(x<=0&&y<=0)
        break;
    }
    ll i=a.size()-1;
    while(1)
    {
        sort(b.begin(),b.end());
        if(a[i]<b[b.size()-1])
        {
            a[i]=b[b.size()-1];
            i++;
            b[b.size()-1]=0;
        }
        else
        i--;
        if(i==-1)
            break;
    }
    for(ll i=a.size()-1;i>=0;i--)
        cout<<a[i];
}


#include <bits/stdc++.h>
#define all(nums) nums.begin(), nums.end()
typedef long long int ll;
const int imax = INT32_MAX;
const int imin = INT32_MIN;
using namespace std;
const ll MOD=1e9+7;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t=1;
    // cin>>t;
    while (t--) {
        string n, s;
        cin>>n>>s;
        sort(all(s), greater<char>());
        int l;
        for (int i=0, l=0; i<n.size(); ++i) {
            if (n[i]<s[l]) {
                n[i]=s[l++];
            }
        }
        cout<<n;
    }
    return 0;
}


#include <bits/stdc++.h>
#define all(nums) nums.begin(), nums.end()
typedef long long int ll;
const int imax = INT32_MAX;
const int imin = INT32_MIN;
using namespace std;
const ll MOD=1e9+7;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t=1;
    // cin>>t;
    while (t--) {
        string n, s;
        cin>>n>>s;
        sort(all(s), greater<char>());
        int l;
        for (int i=n.size()-1, l=0; i>=0; --i) {
            if (n[i]<s[l]) {
                n[i]=s[l++];
            }
        }
        cout<<n;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <climits>
#include <iomanip>
#include <stack>
#include <queue>

#define endl '\n'
#define lf first
#define rg second
#define forn(i, n) for(int i = 0; i < n; ++i)
#define fora(el, a) for (auto el : a)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define YES "YES\n"
#define NO "NO\n"

using namespace std;

typedef long long ll;
typedef pair<int, int> pt;
typedef pair<double, double> pd;
typedef pair<ll, ll> pl;

const int N = 2 * 100 * 1000;
const int INF = 1000 * 1000 * 1000 + 10;
const ll INF64 = 1e18 + 20;
int n, m;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	string s;
	string a;

	cin >> a >> s;

	vector<int> cnt(10);

	forn(i, s.length()) {
		cnt[s[i] - '0']++;
	}

	int cur = 9;

	forn(i, a.length()) {
		while (cnt[cur] == 0 && cur >= 0) cur--;

		if (a[i] - '0' < cur) {
			a[i] = char(cur + '0');
			cnt[cur]--;
		}
	}

	cout << a;
}
close


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <climits>
#include <iomanip>
#include <stack>
#include <queue>

#define endl '\n'
#define lf first
#define rg second
#define forn(i, n) for(int i = 0; i < n; ++i)
#define fora(el, a) for (auto el : a)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define YES "YES\n"
#define NO "NO\n"

using namespace std;

typedef long long ll;
typedef pair<int, int> pt;
typedef pair<double, double> pd;
typedef pair<ll, ll> pl;

const int N = 2 * 100 * 1000;
const int INF = 1000 * 1000 * 1000 + 10;
const ll INF64 = 1e18 + 20;
int n, m;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	string s;
	string a;

	cin >> a >> s;

	vector<int> cnt(10);

	forn(i, s.length()) {
		cnt[s[i] - '0']++;
	}

	int cur = 9;

	forn(i, a.length()) {
		while (cnt[cur] == 0 && cur >= 0) cur--;

		if (a[i] - '0' < cur) {
			a[i] = char(cur + '0');
			cnt[cur]--;
		}
	}

	cout << a;
}


#include<sstream>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
    string s1,s2; cin>>s1>>s2;
    vector<int> v1;
    for(int i=0;i<s2.size();i++)  v1.push_back((s2[i]-'0'));
    sort(v1.begin(),v1.end(),greater<int>());
    stringstream ss;
    for(int i=0,j=0;i<v1.size();i++)
        for(;j<s1.size();j++)
            if(s1[j]-'0'<v1[i]){ ss<<v1[i]; ss>>s1[j];break; }
    cout<<s1;
    return 0;
}


#include<sstream>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
    string s1,s2; cin>>s1>>s2;
    vector<int> v1;
    for(int i=0;i<s2.size();i++)  v1.push_back((s2[i]-'0'));
    sort(v1.begin(),v1.end(),greater<int>());
    stringstream ss;
    for(int i=0,j=0;i<v1.size()-1;i++)
        for(;j<s2.size();j++)
            if(s1[j]-'0'<v1[i]){ ss<<v1[i]; ss>>s1[j];break; }
    cout<<s1;
    return 0;
}


#include<sstream>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
    string s1,s2; cin>>s1>>s2;
    vector<int> v1;
    for(int i=0;i<s2.size();i++)  v1.push_back((s2[i]-'0'));
    sort(v1.begin(),v1.end(),greater<int>());
    stringstream ss;
    for(int i=0,j=0;i<v1.size();i++)
        for(;j<s2.size();j++)
            if(s1[j]-'0'<v1[i]){ ss<<v1[i]; ss>>s1[j];break; }
    cout<<s1;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
   string s1,s2;cin>>s1>>s2;
   set<char>s;
   for(char x:s2) s.insert(x);
   vector<char>v(s.size());
   copy(s.begin(),s.end(),v.begin());
   sort(v.rbegin(),v.rend());
   for(int i=0,m=0;i<s1.size();++i)
   {
       if(m==v.size()) break;
       if(s1[i]<v[m]) s1[i]=v[m++];
   }
   cout<<s1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
   string s1,s2;cin>>s1>>s2;
   sort(s2.rbegin(),s2.rend());
   for(int i=0,m=0;i<s1.size();++i)
   {
       if(m==s2.size()) break;
       if(s1[i]<s2[m]) s1[i]=s2[m++];
   }
   cout<<s1;
}


#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>

#define ll long long
#define scanint1(a) scanf("%d",&a)
#define scanint2(a,b) scanf("%d %d",&a,&b)
#define scanint3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define scanint4(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d)
#define scanint5(a,b,c,d,e) scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
#define scanint6(a,b,c,d,e,f) scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f)
#define scanlong1(a) scanf("%ld",&a)
#define scanlong2(a,b) scanf("%ld %ld",&a,&b)
#define scanlong3(a,b,c) scanf("%ld %ld %ld",&a,&b,&c)
#define scanlong4(a,b,c,d) scanf("%ld %ld %ld %ld",&a,&b,&c,&d)
#define scanlong5(a,b,c,d,e) scanf("%ld %ld %ld %ld %ld",&a,&b,&c,&d,&e)
#define scanll1(a) scanf("%lld",&a)
#define scanll2(a,b) scanf("%lld %lld",&a,&b)
#define scanll3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define scanll4(a,b,c,d) scanf("%lld %lld %lld %lld",&a,&b,&c,&d)
#define scanll5(a,b,c,d,e) scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e)
#define read  freopen("simple.in","r",stdin);
#define write freopen("output.txt","w",stdout);
#define MAXN   10000005

using namespace std;

             int main()
           {

               string s,t;
               cin>>s>>t;
                vector<int> vc;
                priority_queue<int> pq;
                for(char c:s)
                    vc.push_back(c-'0');
                for(char c:t)
                    pq.push(c-'0');

                for(int i=0;i<vc.size();i++)
                {
                    if(!pq.size()) break;
                    if(vc[i]<pq.top())
                        vc[i]=pq.top(),pq.pop();
                }

                for(int i:vc)
                    cout<<i;

               return 0;
           }


#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>

#define ll long long
#define scanint1(a) scanf("%d",&a)
#define scanint2(a,b) scanf("%d %d",&a,&b)
#define scanint3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define scanint4(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d)
#define scanint5(a,b,c,d,e) scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
#define scanint6(a,b,c,d,e,f) scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f)
#define scanlong1(a) scanf("%ld",&a)
#define scanlong2(a,b) scanf("%ld %ld",&a,&b)
#define scanlong3(a,b,c) scanf("%ld %ld %ld",&a,&b,&c)
#define scanlong4(a,b,c,d) scanf("%ld %ld %ld %ld",&a,&b,&c,&d)
#define scanlong5(a,b,c,d,e) scanf("%ld %ld %ld %ld %ld",&a,&b,&c,&d,&e)
#define scanll1(a) scanf("%lld",&a)
#define scanll2(a,b) scanf("%lld %lld",&a,&b)
#define scanll3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define scanll4(a,b,c,d) scanf("%lld %lld %lld %lld",&a,&b,&c,&d)
#define scanll5(a,b,c,d,e) scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e)
#define read  freopen("simple.in","r",stdin);
#define write freopen("output.txt","w",stdout);
#define MAXN   10000005

using namespace std;

             int main()
           {

               string s,t;
               cin>>s>>t;

               for(int x=0;x<s.size();x++)
               {
                   int pos = 0;
                   for(int i=0;i<t.size();i++)
                    if(s[x]<t[i]&&t[pos]<t[i])
                      pos=i;

                    if(s[x]<t[pos]){
                   s[x]=t[pos];
                   t[pos]='0';
                   t[pos]='0';}
               }
            cout<<s<<endl;
               return 0;
           }


#include <bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define mk make_pair
#define pb push_back
#define pp pop_back
using namespace std;
const ll N = 1e12+100 ;
string s , s1 , s2 ;
int j ;
vector<string> v ;
int main()
{
    cin >> s >> s1 ;
    sort(s1.begin(),s1.end()) ;
    reverse(s1.begin(),s1.end()) ;
    s2 = s ;
    v.pb(s) ;
    for ( int i = 0 ; i < s.size()  ; )
    {
        if(s1[j] > s[i] )
        {
            s[i] = s1[j] ;
            ++i ;
            ++j ;
        }
        else
        {
            ++j ;
        }
        if ( j >= s.size() - 1 )
        {
            ++i ;
            j = 0 ;
        }
    }
    cout << s ;
}


#include <bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define mk make_pair
#define pb push_back
#define pp pop_back
using namespace std;
const ll N = 1e12+100 ;
string s , s1 , s2 ;
int j  ;
vector<string> v ;
int main ()
{
    cin >> s >> s1 ;
    sort(s1.begin(),s1.end()) ;
    reverse(s1.begin(),s1.end()) ;
    for ( int i = 0 ; i < s.size() || j < s1.size() ;   )
    {
        if ( s1[j] > s[i] )
        {
            s[i] = s1[j] ;
            j++ ;
        }
        else
        {
            i++ ;
        }
    }
    cout << s ;
}


#include <bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define mk make_pair
#define pb push_back
#define pp pop_back
using namespace std;
const ll N = 1e12+100 ;
string s , s1 , s2 ;
ll j  ;
vector<string> v ;
int main ()
{
    cin >> s >> s1 ;
    sort(s1.begin(),s1.end()) ;
    j = s1.size() - 1 ;
    for ( ll i = 0   ; i < s.size()  ; ++i  )
    {
        if ( s1[j] > s[i] )
        {
            s[i] = s1[j] ;
            j-- ;
        }
    }
    cout << s ;
}


#include <bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define mk make_pair
#define pb push_back
#define pp pop_back
using namespace std;
const ll N = 1e12+100 ;
string s , s1 , s2 ;
ll j  ;
vector<string> v ;
int main ()
{
    cin >> s >> s1 ;
    sort(s1.begin(),s1.end()) ;
    reverse(s1.begin(),s1.end()) ;
    for ( ll i = 0 ; i < s.size() || j < s1.size() ; ++i  )
    {
        if ( s1[j] > s[i] )
        {
            s[i] = s1[j] ;
            j++ ;
        }
    }
    cout << s ;
}


#include <bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define mk make_pair
#define pb push_back
#define pp pop_back
using namespace std;
const ll N = 1e12+100 ;
string s , s1 , s2 ;
vector<string> v ;
int main()
{
    cin >> s >> s1 ;
    s2 = s ;
    v.pb(s) ;
    for ( int i = 0 ; i < s1.size() ; ++i )
    {
        if ( s1[i] > s[i] )
        {
            s[i] = s1[i] ;
            v.pb(s) ;
            s = s2 ;
        }
    }
    sort(v.begin(),v.end()) ;
    cout << v[v.size()-1] ;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a,b;
    cin >> a >> b;
    for(int i = 0 ; i < b.size() ; i++)
    {
        for(int j = 0 ; j < a.size() ; j++)
        {
            if(a[j] < b[i])
            {
                a[j] = b[i];
                break;
            }
        }
    }
    cout << a << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a,s;
    cin >> a >> s;
    for(int i = 0 ; i < s.size() ; i++)
    {
        for(int j = 0 ; j < a.size() ; j++)
        {
            if(a[j] < s[i]) a[j] = s[i];
        }
    }
    cout << a << endl ;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a,b;
    cin >> a >> b;
    for(int i = 0 ; i < b.size() ; i++)
    {
        for(int j = 0 ; j < a.size() ; j++)
        {
            if(a[j] < b[i])
            {
                a[i] = b[i];
                break;
            }
        }
    }
    cout << a << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a,b;
    int k;
    cin >> a >> b;
    k = b.size()-1;
    sort(b.begin(),b.end());

    for(int i = 0 ; i < a.size() ; i++)
    {
        if(a[i] < b[k])
        {
            a[i] = b[k];
            k--;
        }
    }
    cout << a  << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define init(a, b) memset(a, b, sizeof(a))
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef unordered_map<int,int> uoii;
typedef unordered_map<string,int> uosi;
typedef unordered_set<int> uoi;
typedef unordered_set<string> uos;
#define MOD = 1e9+7;

const int INF = 1e10;
const int N =100;

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    IOS
    string a,s;
    cin>>a>>s;
    int n=a.length();
    int m=s.length();
    priority_queue<pii> pq;
    map<int,int>mp;
    for(int i=0;i<m;i++){
        mp[s[i]-'0']++;
    }
    for(auto it=mp.begin();it!=mp.end();it++){
        pq.push({it->first,it->second});
      //  cout<<it->first<<" "<<it->second<<'\n';
    }
    int used=0;
    for(int i=0;i<n&&used<n-1;i++){
      //  cout<<i<<'\n';
        pii top=pq.top();
        //cout<<a[i]<<" "<<top.fi<<'\n';
        if(a[i]-'0'<top.fi){
          //  cout<<"IN\n";
            used++;
            pq.pop();
            a[i]=top.fi+'0';
            pq.push({top.fi,--top.se});
        }
    }
    cout<<a;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define init(a, b) memset(a, b, sizeof(a))
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef unordered_map<int,int> uoii;
typedef unordered_map<string,int> uosi;
typedef unordered_set<int> uoi;
typedef unordered_set<string> uos;
#define MOD = 1e9+7;

const int INF = 1e10;
const int N =100;

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    IOS
    string a,s;
    cin>>a>>s;
    int n=a.length();
    int m=s.length();
    priority_queue<pii> pq;
    map<int,int>mp;
    for(int i=0;i<m;i++){
        mp[s[i]-'0']++;
    }
    for(auto it=mp.begin();it!=mp.end();it++){
        pq.push({it->first,it->second});
      //  cout<<it->first<<" "<<it->second<<'\n';
    }
    int used=0;
    for(int i=0;i<n;i++){
      //  cout<<i<<'\n';
        pii top=pq.top();
       // cout<<a[i]<<" "<<top.fi<<" "<<top.se<<'\n';
        if(a[i]-'0'<top.fi&&top.se>0){
           // cout<<"IN\n";
            used++;
            pq.pop();
            a[i]=top.fi+'0';
            if(top.se>1)
                pq.push({top.fi,--top.se});
        }
    }
    cout<<a;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define init(a, b) memset(a, b, sizeof(a))
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef unordered_map<int,int> uoii;
typedef unordered_map<string,int> uosi;
typedef unordered_set<int> uoi;
typedef unordered_set<string> uos;
#define MOD = 1e9+7;

const int INF = 1e10;
const int N =100;

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    IOS
    string a,s;
    cin>>a>>s;
    int n=a.length();
    int m=s.length();
    priority_queue<pii> pq;
    map<int,int>mp;
    for(int i=0;i<m;i++){
        mp[s[i]-'0']++;
    }
    for(auto it=mp.begin();it!=mp.end();it++){
        pq.push({it->first,it->second});
      //  cout<<it->first<<" "<<it->second<<'\n';
    }
    int used=0;
    for(int i=0;i<n&&used<m-1;i++){
      //  cout<<i<<'\n';
        pii top=pq.top();
       // cout<<a[i]<<" "<<top.fi<<" "<<top.se<<'\n';
        if(a[i]-'0'<top.fi&&top.se>0){
           // cout<<"IN\n";
            used++;
            pq.pop();
            a[i]=top.fi+'0';
            if(top.se>1)
                pq.push({top.fi,--top.se});
        }
    }
    cout<<a;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define init(a, b) memset(a, b, sizeof(a))
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef unordered_map<int,int> uoii;
typedef unordered_map<string,int> uosi;
typedef unordered_set<int> uoi;
typedef unordered_set<string> uos;
#define MOD = 1e9+7;

const int INF = 1e10;
const int N =100;

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    IOS
    string a,s;
    cin>>a>>s;
    int n=a.length();
    int m=s.length();
    priority_queue<pii> pq;
    map<int,int>mp;
    for(int i=0;i<m;i++){
        mp[s[i]-'0']++;
    }
    for(auto it=mp.begin();it!=mp.end();it++){
        pq.push({it->first,it->second});
      //  cout<<it->first<<" "<<it->second<<'\n';
    }
    int used=0;
    for(int i=0;i<n&&used<m;i++){
      //  cout<<i<<'\n';
        pii top=pq.top();
       // cout<<a[i]<<" "<<top.fi<<" "<<top.se<<'\n';
        if(a[i]-'0'<top.fi&&top.se>0){
           // cout<<"IN\n";
            used++;
            pq.pop();
            a[i]=top.fi+'0';
            if(top.se>1)
                pq.push({top.fi,--top.se});
        }
    }
    cout<<a;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define init(a, b) memset(a, b, sizeof(a))
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef unordered_map<int,int> uoii;
typedef unordered_map<string,int> uosi;
typedef unordered_set<int> uoi;
typedef unordered_set<string> uos;
#define MOD = 1e9+7;

const int INF = 1e10;
const int N =100;

int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    IOS
    string a,s;
    cin>>a>>s;
    int n=a.length();
    int m=s.length();
    priority_queue<pii> pq;
    map<int,int>mp;
    for(int i=0;i<m;i++){
        mp[s[i]-'0']++;
    }
    for(auto it=mp.begin();it!=mp.end();it++){
        pq.push({it->first,it->second});
      //  cout<<it->first<<" "<<it->second<<'\n';
    }
    int used=0;
    for(int i=0;i<n&&used<n-1;i++){
      //  cout<<i<<'\n';
        pii top=pq.top();
        //cout<<a[i]<<" "<<top.fi<<'\n';
        if(a[i]-'0'<top.fi&&top.se>0){
          //  cout<<"IN\n";
            used++;
            pq.pop();
            a[i]=top.fi+'0';
            pq.push({top.fi,--top.se});
        }
    }
    cout<<a;

    return 0;
}


#include <bits/stdc++.h>

using namespace std ;

int main()
{
	string a , s ;
	cin >> a >> s;

	sort(s.begin() , s.end());

	int j = s.size()-1 ;

	for(int i=0 ; i < a.size() ; i++)
	{
		if(s[j] > a[i])
		a[i] = s[j--] ;
	}

	cout << a ;
}


#include <bits/stdc++.h>

using namespace std ;

int main()
{
	string a , s ;
	cin >> a >> s;

	sort(s.begin() , s.end());

	for(int i=0 ; i < a.size() ; i++)
	{
		if(s[s.size()-1] > a[i])
		a[i] = s[s.size()-1] ;
	}

	cout << a ;
}


#include <bits/stdc++.h>
using namespace std;
int g[10];

int main(){
	string a,b;
	cin >> a >> b;
	for(int i = 0 ; i < b.size() ; i += 1){
		g[b[i]-'0'] += 1;
	}
	for(int i = 0 ; i < a.size() ; i += 1){
		int v = a[i]-'0';
		for(int j = 9 ; j > v ; j -= 1){
			if(g[j] > 0){
				v = j;
				break;
			}
		}
		cout << v;
	}
	cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
int g[10];

int main(){
	string a,b;
	cin >> a >> b;
	for(int i = 0 ; i < b.size() ; i += 1){
		g[b[i]-'0'] += 1;
	}
	for(int i = 0 ; i < a.size() ; i += 1){
		int v = a[i]-'0';
		for(int j = 9 ; j > v ; j -= 1){
			if(g[j] > 0){
				v = j;
				g[j] -= 1;
				break;
			}
		}
		cout << v;
	}
	cout << endl;
}


#include <bits/stdc++.h>
#include<math.h>
using namespace std;

#define ll long long

vector <ll> v;
vector <ll> vv;
set <ll> st;

ll x,y,z;
string s,s1,s2,s3,s4,s5;

void memset_ll(ll a[], ll v, ll n)
{
    for(ll i=0; i<n; i++)
        a[i]=v;
}
void memset_s(char a[], char v, ll n)
{
    for(ll i=0; i<n; i++)
        a[i]=v;
}

int compare(const void * a, const void * b)
{
    return ( *(ll*)a - *(ll*)b );
}
void q_sort(ll a[], ll n)
{
    qsort(a, n, sizeof(ll), compare);
}

static int compare1 (const void * a, const void * b)
{
    if (*(double*)a > *(double*)b)
        return 1;
    else if (*(double*)a < *(double*)b)
        return -1;
    else
        return 0;
}
void q_sort_d(double a[], ll n)
{
    qsort(a, n, sizeof(double), compare1);
}

string trans_upper(string ss)
{
    transform(ss.begin(),ss.end(),ss.begin(), ::toupper);
    return ss;
}

ll power(ll a, ll b)
{
    ll pow = 1;
    while ( b )
    {
        if ( b & 1 )
        {
            pow = pow * a;
            --b;
        }
        a = a*a;
        b = b/2;
    }
    return pow;
}

ll bsearch(ll arr[], ll l, ll r, ll x)
{
    if (r >= l)
    {
        ll mid = l + (r - l) / 2;
        if (arr[mid] == x)
            return mid;
        if (arr[mid] > x)
            return bsearch(arr, l, mid - 1, x);
        return bsearch(arr, mid + 1, r, x);
    }
    return -1;
}

ll fact(ll n)
{
    ll f=1,i;
    for(i=1; i<=n; i++)
        f=f*i;
    return f;
}

ll is_prime(ll n)
{
    ll i,j,k=1;
    for(i=2; i<=n/2; i++)
    {
        if(n%i==0)
        {
            k=0;
            break;
        }
    }
    return k;
}

ll vow(char c)
{
    ll k=1;
    ll l=0;
    if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
        return k;
    else
        return l;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL) ;
    cout.tie(NULL) ;

    ll t,l,p,q,n,m,o=0,r,i,j,h,k,e=0,d=0,f=0,g=0;
    ll temp=0,nl,np;
    double p1,p2=0.0,p3=0.0,p4,p5,p6,pp=0.0,ppp;
    p1=3.1415926536;
    ll x1,x2,y1,y2,uu=0,dd=0,rr=0,lll=0;
    cin>>s>>s1;
    n=s.size();
    l=s1.size();
    sort(s1.begin(),s1.end());
    for(i=l-1,j=0;i>0,j<n;)
    {
        if(s1[i]>s[j])
        {
            s[j]=s1[i];
            j++;i--;
        }
        else
        {
            j++;
        }
    }
    cout<<s;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define trav(a, A) for (auto & a : A)
#define f first
#define s second
#define all(x) begin(x), end(x)
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

typedef pair<int, int> pii;
typedef vector<int> vi;
using graph = vector<vi>;
typedef long long ll;

ostream& operator<<(ostream& os, const pii& p){
	os <<  "(" << p.f << ", " << p.s << ")";
	return os;
}
ostream& operator<<(ostream& os, const vi& p){
	for (auto a: p) os << a << ' ';
	return os;
}
ostream& operator<<(ostream& os, const graph& g){
	for (int i = 0; i < g.size(); ++i, os << endl){
		os << i << ": "<< g[i];
	}
	return os;
}

const ll MOD = 1e+9+7;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const ll MAXN = 1e+5+7;
int freq[111];
void solve(){
    string a, s;
    cin >> a >> s;
    int n = a.size();
    // cout << "OI" << endl;
    for (auto x: s) freq[x-'0']++;
    for (int i = 0; i < n; ++i){
        for (int j = 9; j > a[i]-'0'; --j){
            if (freq[j]){
                freq[j]--;
                a[i] = j+'0';
                break;
            }
        }
    }
    cout << a << '\n';
}

int main ()
{
	ios_base::sync_with_stdio (0);
	cin.tie (0);
	ll i, j, n;
	int t = 1; //cin >> t;
	while(t--) solve();
	return 0;
}


#include<bits/stdc++.h>
typedef long long LL;
using namespace std;

char A[100001];
char S[100001];
int F[10];
bool used[100001];
int n,m;

int main()
{
    scanf("%s",A);
    scanf("%s",S);
    n = strlen(A);
    m = strlen(S);

    for(int i=0;i<m;i++) F[S[i]-'0']++;

    for(int i=0;i<n;i++)
    {
        bool ok = false;
        for(int j=9;j>0;j--)
        {
            if(F[j]>0 && (A[i]-'0')<j)
            {
                A[i] = (char)(j+'0');
                F[j]--;
                ok = true;
            }
            if(ok) break;
        }
    }

    printf("%s\n",A);

	return 0;
}


#pragma GCC optimze("Ofast")

#include<bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define pb push_back
#define vi vector<ll>
#define vs vector<string>
#define vp(x,y) vector< pair<x,y> >
#define mp make_pair
#define all(x) x.begin(),x.end()
#define Sort(x) sort(all(x));
#define DSort(x) sort(all(x), greater<>());
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define l_b lower_bound
#define u_b upper_bound
#define u_set unordered_set
#define u_map unordered_map
#define ff first
#define sc second
#define fast_io ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0)

const ll M=1e9+7;
const ld PI=3.14159265;

using namespace std;

int main(){
    fast_io;
    ll t,i,pos=0;
    t=1;
    string n,ref1;
    while(t--){
        cin>>n>>ref1;
        sort(ref1.begin(),ref1.end(),greater<>());
        pos=0;
        for(i=0;i<n.length();i++){
            if((n[i]-'0')<(ref1[pos]-'0')){
                n[i]=ref1[pos];
                pos++;
            }
        }
        cout<<n;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll  long long
struct triple{ll   x=0;ll   y=0;ll   z=0;void mt(ll   a,ll   b, ll   c){x=a;y=b;z=c;} void holify(){x=69;y=420;z=911;}};
#define pb push_back
#define x first
#define y second
#define vec vector
#define pf(vec,val) vec.insert(vec.begin(),val)
#define mp make_pair
#define pii pair<ll  ,ll  >
#define maxll  69696991169
#define minll  -69696942069
#define sz(vec) (ll )vec.size()
#define speedup ios_base::sync_with_stdio(0); cin.tie(NUll ); cout.tie(NUll );
#define loop(mep) for(auto it = mep.begin();it!=mep.end();++it)
//#define usaco_crap(str)ifstream cin((string)str + (string)".in"); ofstream cout ((string)str+(string)".out");
#define range(n) =0;i<(n);i++
#define prll  (vec) for(auto it=vec.begin();it!=vec.end();++it)cerr<<*it<<" ";cerr<<endl;
// mt make triple // triple.mt(a,b,c) //
bool srt(pair<char,int>a,pair<char,int>b){
	return a.y < b.y;
}
int main(){
	string a,b;
	cin>>a>>b;
	vector<pair<char , int>>arr;
	sort(b.begin(),b.end());
	for(int i = 0;i<a.size();i++){
		arr.pb(mp(a[i] , i));
	}
	sort(arr.begin(),arr.end());
	for(int i = 0 ; i < a.size() ; i++){
		if(b.back() > arr[i].x){
			arr[i].x = b.back();
			b.pop_back();
		}
	}
	sort(arr.begin(),arr.end(),srt);
	for(pii i : arr){
		cout<<char(i.x);
	}cout<<endl;
}


#include <bits/stdc++.h>

using namespace std;

int n , m ;
string a[200];
int dis[200][10009];

int main()
{

       cin>>n>>m;

       for(int i=0;i<n;i++){
        cin>>a[i];

       if( a[i].find('1')==string::npos ){ cout<<"-1"<<endl;return 0; }

       }

for(int i=0;i<n;i++){

int nearest=-1000000;

for(int j=0;j<m;j++){

if( a[i][j]=='1' ){ nearest=j; }
dis[i][j]=j-nearest;

}

nearest=1000000;
for(int j=m-1;j>=0;j--){
if( a[i][j]=='1'){ nearest=j;}
  dis[i][j]=min(dis[i][j],nearest-j);
}

}

int best_ans=1000000000;

for(int j=0;j<m;j++){
int ans=0;

for(int i=0;i<n;i++)
        ans+=dis[i][j];

best_ans=min(ans,best_ans);
}

   cout<<best_ans<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int INF = 1000000000;
int n , m ;
string a[200];
int dis[200][10009];

int main()
{

       cin>>n>>m;

       for(int i=0;i<n;i++){
        cin>>a[i];

       if( a[i].find('1')==string::npos ){ cout<<"-1"<<endl;return 0; }

       }

for(int i=0;i<n;i++)
    for(int j=0;j<m;j++)
    dis[i][j]=INF;

for(int i=0;i<n;i++){

int nearest=-1*INF;

for(int j=0;j<2*m;j++){

if( a[i][(j%m)]=='1' ){ nearest=(j%m); }
dis[i][(j%m)]=min(dis[i][(j%m)], min( abs((j%m)-nearest) , abs(nearest+m-(j%m)) )  );

}

nearest=INF;
for(int j=2*m-1;j>=0;j--){
if( a[i][(j%m)]=='1'){ nearest=(j%m);}
  dis[i][(j%m)]=min(dis[i][(j%m)],min(abs(nearest-(j%m)) , abs(  (j%m)-nearest+m ) ) );
}

}

int best_ans=INF;

for(int j=0;j<m;j++){
int ans=0;

for(int i=0;i<n;i++)
        ans+=dis[i][j];

best_ans=min(ans,best_ans);
}

   cout<<best_ans<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n , m ;
string a[200];
int dis[200][10009];

int main()
{

       cin>>n>>m;

       for(int i=0;i<n;i++){
        cin>>a[i];

       if( a[i].find('1')==string::npos ){ cout<<"-1"<<endl;return 0; }

       }

for(int i=0;i<n;i++)for(int j=0;j<m;j++)dis[i][j]=1000000000;

for(int i=0;i<n;i++){

int nearest=-100000000;

for(int j=0;j<2*m;j++){

if( a[i][(j%m)]=='1' ){ nearest=(j%m); }
dis[i][(j%m)]=min(dis[i][(j%m)], min( abs((j%m)-nearest) , abs(nearest+m-(j%m)) )  );

}

nearest=100000000;
for(int j=2*m-1;j>=0;j--){
if( a[i][(j%m)]=='1'){ nearest=(j%m);}
  dis[i][(j%m)]=min(dis[i][(j%m)],min(abs(nearest-(j%m)) , abs(  (j%m)-nearest+m ) ) );
}

}

int best_ans=1000000000;

for(int j=0;j<m;j++){
int ans=0;

for(int i=0;i<n;i++)
        ans+=dis[i][j];

best_ans=min(ans,best_ans);
}

   cout<<best_ans<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n , m ;
string a[200];
int dis[200][10009];

int main()
{

       cin>>n>>m;

       for(int i=0;i<n;i++){
        cin>>a[i];

       if( a[i].find('1')==string::npos ){ cout<<"-1"<<endl;return 0; }

       }

for(int i=0;i<n;i++)for(int j=0;j<m;j++)dis[i][j]=100000000;

for(int i=0;i<n;i++){

int nearest=-1000000;

for(int j=0;j<m;j++){

if( a[i][j]=='1' ){ nearest=j; }
dis[i][j]=min(dis[i][j], min( abs(j-nearest) , abs(nearest+m-j) )  );

}

nearest=1000000;
for(int j=m-1;j>=0;j--){
if( a[i][j]=='1'){ nearest=j;}
  dis[i][j]=min(dis[i][j],min(abs(nearest-j) , abs(  j-nearest+m ) ) );
}

}

int best_ans=1000000000;

for(int j=0;j<m;j++){
int ans=0;

for(int i=0;i<n;i++)
        ans+=dis[i][j];

best_ans=min(ans,best_ans);
}

   cout<<best_ans<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int INF = 1000000000;
const int N = 111 ;
const int M = 22222 ;
int n , m ;
char a[N][M];
int dis[N][M];

int main()
{

       cin>>n>>m;

       for(int i=0;i<n;i++){
        cin>>a[i];

bool ok=0;

       for(int j=m;j<2*m;j++)
        {a[i][j]=a[i][j-m];
        if(a[i][j]=='1')ok=1;
        }

       if( ! ok  ){cout<<"-1"<<endl; return 0;}

       }

memset( dis , -1 , sizeof dis );

for(int i=0;i<n;i++){

int nearest=-1;

for(int j=0;j<2*m;j++){

if( a[i][j]=='1' ) nearest=j;

if(nearest != -1)
if( dis[i][j%m]==-1 || dis[i][j%m] > j-nearest )
    dis[i][j%m]=j-nearest;
}

nearest=-1;
for(int j=2*m-1;j>=0;j--){

if( a[i][(j)]=='1') nearest=(j);

if(nearest != -1)
if( a[i][j%m]==-1 || dis[i][j%m]> nearest-j )
    dis[i][j%m]=nearest-j;
}

}

int best_ans=INF;

for(int j=0;j<m;j++){
int ans=0;

for(int i=0;i<n;i++)
        ans+=dis[i][j];

best_ans=min(ans,best_ans);
}

   cout<<best_ans<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n , m ;
string a[200];
int dis[200][10009];

int main()
{

       cin>>n>>m;

       for(int i=0;i<n;i++){
        cin>>a[i];

       if( a[i].find('1')==string::npos ){ cout<<"-1"<<endl;return 0; }

       }

for(int i=0;i<n;i++){

int nearest=-1000000;

for(int j=0;j<m;j++){

if( a[i][j]=='1' ){ nearest=j; }
dis[i][j]=j-nearest;

}

nearest=1000000;
for(int j=m-1;j>=0;j--){
if( a[i][j]=='1'){ nearest=j;}
  dis[i][j]=min(dis[i][j],nearest-j);
}

dis[i][0]=min(dis[i][0],1+dis[i][m-1]);
dis[i][m-1]=min(dis[i][m-1],1+dis[i][0]);

}

int best_ans=1000000000;

for(int j=0;j<m;j++){
int ans=0;

for(int i=0;i<n;i++)
        ans+=dis[i][j];

best_ans=min(ans,best_ans);
}

   cout<<best_ans<<endl;

    return 0;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

const int maxn = 1e2+8;
const int maxm = 1e4+9;
const int inf = 1e9+10;

int a[maxn][maxm] , l[maxn][maxm] , r[maxn][maxm];

main(){
    int n , m; cin >> n >> m;
    for(int i = 1;i <= n;i++){
        string s; cin >> s;
        for(int j = 1;j <= m;j++)
            a[i][j] = s[j-1] - '0';
    }
    for(int i = 1;i <= n;i++)
        for(int j = 0;j <= m+1;j++)
            l[i][j] = inf , r[i][j] = inf;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            if(a[i][j] == 1) l[i][j] = 0;
            else l[i][j] = l[i][j-1] + 1;
        }
        for(int j = m;j >= 1;j--){
            if(a[i][j] == 1) r[i][j] = 0;
            else r[i][j] = r[i][j+1] + 1;
        }
        for(int j = 1;j <= m;j++) r[i][j] = min(r[i][j] , j + l[i][m]);
        for(int j = 1;j <= m;j++) l[i][j] = min(l[i][j] , (n-j+1) + r[i][1]);
    }

    int ans = inf;

    for(int i = 1;i <= m;i++){
        int sum = 0;
        for(int j = 1;j <= n;j++) sum += min(l[j][i] , r[j][i]);
        ans = min(ans , sum);
    }
    if(ans == inf) return cout << -1 << endl , 0;
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

const int maxn = 1e2+8;
const int maxm = 1e4+9;
const int inf = 1e9+10;

int a[maxn][maxm] , l[maxn][maxm] , r[maxn][maxm];

main(){
    int n , m; cin >> n >> m;
    for(int i = 1;i <= n;i++){
        string s; cin >> s;
        for(int j = 1;j <= m;j++)
            a[i][j] = s[j-1] - '0';
    }
    for(int i = 1;i <= n;i++)
        for(int j = 0;j <= m+1;j++)
            l[i][j] = inf , r[i][j] = inf;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            if(a[i][j] == 1) l[i][j] = 0;
            else l[i][j] = l[i][j-1] + 1;
        }
        for(int j = m;j >= 1;j--){
            if(a[i][j] == 1) r[i][j] = 0;
            else r[i][j] = r[i][j+1] + 1;
        }
        for(int j = 1;j <= m;j++) l[i][j] = min(l[i][j] , j + l[i][m]);
        for(int j = 1;j <= m;j++) r[i][j] = min(r[i][j] , (m-j+1) + r[i][1]);
    }
    int ans = inf;

    for(int i = 1;i <= m;i++){
        int sum = 0;
        for(int j = 1;j <= n;j++) sum += min(l[j][i] , r[j][i]);
        ans = min(ans , sum);
    }
    if(ans == inf) return cout << -1 << endl , 0;
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

const int maxn = 1e2+8;
const int maxm = 1e4+9;
const int inf = 1e9+10;

int a[maxn][maxm] , l[maxn][maxm] , r[maxn][maxm];

main(){
    int n , m; cin >> n >> m;
    for(int i = 1;i <= n;i++){
        string s; cin >> s;
        for(int j = 1;j <= m;j++)
            a[i][j] = s[j-1] - '0';
    }
    for(int i = 1;i <= n;i++)
        for(int j = 0;j <= m+1;j++)
            l[i][j] = inf , r[i][j] = inf;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            if(a[i][j] == 1) l[i][j] = 0;
            else l[i][j] = l[i][j-1] + 1;
        }
        for(int j = m;j >= 1;j--){
            if(a[i][j] == 1) r[i][j] = 0;
            else r[i][j] = r[i][j+1] + 1;
        }
        for(int j = 1;j <= m;j++) l[i][j] = min(l[i][j] , j + l[i][m]);
        for(int j = 1;j <= m;j++) r[i][j] = min(r[i][j] , (n-j+1) + r[i][0]);
    }
    int ans = inf;

    for(int i = 1;i <= m;i++){
        int sum = 0;
        for(int j = 1;j <= n;j++) sum += min(l[j][i] , r[j][i]);
        ans = min(ans , sum);
    }
    if(ans == inf) return cout << -1 << endl , 0;
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

const int maxn = 1e2+8;
const int maxm = 1e4+9;
const int inf = 1e9+10;

int a[maxn][maxm] , l[maxn][maxm] , r[maxn][maxm];

main(){
    int n , m; cin >> n >> m;
    for(int i = 1;i <= n;i++){
        string s; cin >> s;
        for(int j = 1;j <= m;j++)
            a[i][j] = s[j-1] - '0';
    }
    for(int i = 1;i <= n;i++)
        for(int j = 0;j <= m+1;j++)
            l[i][j] = inf , r[i][j] = inf;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            if(a[i][j] == 1) l[i][j] = 0;
            else l[i][j] = l[i][j-1] + 1;
        }
        for(int j = m;j >= 1;j--){
            if(a[i][j] == 1) r[i][j] = 0;
            else r[i][j] = r[i][j+1] + 1;
        }
        for(int j = 1;j <= m;j++) r[i][j] = min(r[i][j] , j + l[i][m]);
        for(int j = 1;j <= m;j++) l[i][j] = min(l[i][j] , (n-j+1) + r[i][0]);
    }
    int ans = inf;

    for(int i = 1;i <= m;i++){
        int sum = 0;
        for(int j = 1;j <= n;j++) sum += min(l[j][i] , r[j][i]);
        ans = min(ans , sum);
    }
    if(ans == inf) return cout << -1 << endl , 0;
    cout << ans << endl;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <string>
using namespace std;
#define min(a,b) (a < b) ? a:b

string arr[101];
long long dp[101][1001];

int val(int a,int b){
    return (fabs(a-b));
}

int func(int a,int m){
    a = a%m;
    if (a < 0) a+= m;
    return a;
}

int main() {
    // your code goes here
    int n,m;
    while(scanf("%d%d",&n,&m) == 2){

        bool final = true;
        for(int i = 0; i < n; ++i){
            bool flag = false;
            cin >> arr[i];
            for(int j = 0; j < m; ++j)
                arr[i][j] -= 48,
                dp[i][j] = 1e9, flag |= arr[i][j];
            final &= flag;
        }

        if (!final){
            printf("-1\n");
            continue;
        }

        for(int x = 0; x < n; ++x){
            int i = 0, j = 0, sum = 0;
            while(i < m){
                while(sum+arr[x][j%m] < 1) sum += arr[x][j%m], ++j;
                dp[x][i] = min(dp[x][i],val(j%m,i));
                sum -= arr[x][i++];
            }
            i = n-1, j = n-1, sum = 0;

            while(i >= 0){
                while(sum + arr[x][func(j,m)] < 1) sum += arr[x][func(j,m)], --j;
                dp[x][i] = min(dp[x][i],val(func(j,m),i));
                sum -= arr[x][i--];
            }
        }

        for(int i = 1; i < n; ++i)
            for(int j = 0; j < m; ++j) dp[i][j] += dp[i-1][j];
        long long ans = 1e9;
        for(int i = 0; i < m; ++i)
            ans = min(ans,dp[n-1][i]);
        printf("%lld\n",ans);
    }
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
#define min(a,b) (a < b) ? a:b

char arr[101][10005];
long long dp[101][1001];
bool flag[101];

int val(int a,int b){
    return (fabs(a-b));
}

int func(int a,int m){
    a = a%m;
    if (a < 0) a+= m;
    return a;
}

int main() {
    // your code goes here
    int n,m;
    while(cin >> n >> m){
        for(int i = 0; i < n; ++i) flag[i] = false;

        for(int i = 0; i < n; ++i)
            for(int j = 0; j < m; ++j) cin >> arr[i][j], arr[i][j] -= 48 ,dp[i][j] = 1e9, flag[i] |= arr[i][j];

        bool final = true;
        for(int i = 0; i < n; ++i)
            final &= flag[i];

        if (!final){
            cout << "-1\n";
            continue;
        }

        for(int x = 0; x < n; ++x){
            int i = 0, j = 0, sum = 0;
            while(i < m){
                while(sum+arr[x][j%m] < 1) sum += arr[x][j%m], ++j;
                dp[x][i] = min(dp[x][i],val(j%m,i));
                sum -= arr[x][i++];
            }
            i = n-1, j = n-1, sum = 0;

            while(i >= 0){
                while(sum + arr[x][func(j,m)] < 1) sum += arr[x][func(j,m)], --j;
                dp[x][i] = min(dp[x][i],val(func(j,m),i));
                sum -= arr[x][i--];
            }
        }

        for(int i = 1; i < n; ++i)
            for(int j = 0; j < m; ++j) dp[i][j] += dp[i-1][j];
        long long ans = 1e9;
        for(int i = 0; i < m; ++i)
            ans = min(ans,dp[n-1][i]);
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <string>
using namespace std;
#define min(a,b) (a < b) ? a:b

string arr[101];
long long dp[101][10001];

int val(int a,int b){
    return (fabs(a-b));
}

int func(int a,int m){
    a = a%m;
    if (a < 0) a+= m;
    return a;
}

int main() {
    // your code goes here
    int n,m;
    while(cin >> n >> m){

        bool final = true;
        for(int i = 0; i < n; ++i){
            bool flag = false;
            cin >> arr[i];
            for(int j = 0; j < m; ++j)
                arr[i][j] -= 48,
                dp[i][j] = 1e9, flag |= arr[i][j];
            final &= flag;
        }

        if (!final){
            printf("-1\n");
            continue;
        }

        for(int x = 0; x < n; ++x){
            int i = 0, j = 0, sum = 0;

            //cout << x << " forward\n";
            while(i < m){
                while(sum+arr[x][j%m] < 1) sum += arr[x][j%m], ++j;
            //  cout << i << " " << j << endl;
                dp[x][i] = min(dp[x][i],val(j,i));
                sum -= arr[x][i++];
            }
            i = m-1, j = m-1, sum = 0;

            //cout << x << " backward\n";
            while(i >= 0){
                while(sum + arr[x][func(j,m)] < 1) sum += arr[x][func(j,m)], --j;
            //  cout << i << " " << j << endl;
                dp[x][i] = min(dp[x][i],val(j,i));
                sum -= arr[x][i--];
            }
        }

        for(int i = 1; i < n; ++i)
            for(int j = 0; j < m; ++j) dp[i][j] += dp[i-1][j];
        long long ans = 1e9;
        for(int i = 0; i < m; ++i)
            ans = min(ans,dp[n-1][i]);
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <string>
using namespace std;
#define min(a,b) (a < b) ? a:b

string arr[101];
long long dp[101][10001];

int val(int a,int b){
    return (fabs(a-b));
}

int func(int a,int m){
    a = a%m;
    if (a < 0) a+= m;
    return a;
}

int main() {
    // your code goes here
    int n,m;
    while(cin >> n >> m){

        bool final = true;
        for(int i = 0; i < n; ++i){
            bool flag = false;
            cin >> arr[i];
            for(int j = 0; j < m; ++j)
                arr[i][j] -= 48,
                dp[i][j] = 1e9, flag |= arr[i][j];
            final &= flag;
        }

        if (!final){
            printf("-1\n");
            continue;
        }

        for(int x = 0; x < n; ++x){
            int i = 0, j = 0, sum = 0;
            while(i < m){
                while(sum+arr[x][j%m] < 1) sum += arr[x][j%m], ++j;
                dp[x][i] = min(dp[x][i],val(j%m,i));
                sum -= arr[x][i++];
            }
            i = n-1, j = n-1, sum = 0;

            while(i >= 0){
                while(sum + arr[x][func(j,m)] < 1) sum += arr[x][func(j,m)], --j;
                dp[x][i] = min(dp[x][i],val(func(j,m),i));
                sum -= arr[x][i--];
            }
        }

        for(int i = 1; i < n; ++i)
            for(int j = 0; j < m; ++j) dp[i][j] += dp[i-1][j];
        long long ans = 1e9;
        for(int i = 0; i < m; ++i)
            ans = min(ans,dp[n-1][i]);
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <string>
using namespace std;
#define min(a,b) (a < b) ? a:b

string arr[101];
long long dp[101][10001];

int val(int a,int b){
    return (fabs(a-b));
}

int func(int a,int m){
    a = a%m;
    if (a < 0) a+= m;
    return a;
}

int main() {
    // your code goes here
    int n,m;
    while(cin >> n >> m){

        bool final = true;
        for(int i = 0; i < n; ++i){
            bool flag = false;
            cin >> arr[i];
            for(int j = 0; j < m; ++j)
                arr[i][j] -= 48,
                dp[i][j] = 1e9, flag |= arr[i][j];
            final &= flag;
        }

        if (!final){
            printf("-1\n");
            continue;
        }

        for(int x = 0; x < n; ++x){
            int i = 0, j = 0, sum = 0;
            while(i < m){
                while(sum+arr[x][j%m] < 1) sum += arr[x][j%m], ++j;
                dp[x][i] = min(dp[x][i],val(j,i));
                sum -= arr[x][i++];
            }
            i = n-1, j = n-1, sum = 0;

            while(i >= 0){
                while(sum + arr[x][func(j,m)] < 1) sum += arr[x][func(j,m)], --j;
                dp[x][i] = min(dp[x][i],val(j,i));
                sum -= arr[x][i--];
            }
        }

        for(int i = 1; i < n; ++i)
            for(int j = 0; j < m; ++j) dp[i][j] += dp[i-1][j];
        long long ans = 1e9;
        for(int i = 0; i < m; ++i)
            ans = min(ans,dp[n-1][i]);
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < (n); i++)

int n, m, dist[105][10005], col[10005];
string line[105];

int main(){
    scanf("%d %d", &n, &m);
    forn(i, n) cin >> line[i];
    forn(i, 105) forn(j, 10005) dist[i][j] = 1e9;
    forn(i, n) forn(j, m){
        if(line[i][j] == '1'){
            dist[i][j] = 0;
            int c = 0;
            for(int k = 1; k <= m; k++){
                if(line[i][(j+k)%m] == 1) break;
                if(dist[i][ (j+k)%m ] > dist[i][j] + k){
                    dist[i][ (j+k)%m ] = dist[i][j] + k;
                }
                else break;
            }
            for(int k = 1; k <= m; k++){
                if(line[i][ ((j-k)+30*m)%m ] == 1) break;
                if(dist[i][ ((j-k)+30*m)%m ] > dist[i][j] + k){
                    dist[i][ ((j-k)+30*m)%m ] = dist[i][j] + k;
                }
                else break;
            }
        }
    }
    forn(i, n) forn(j, m){
        if(dist[i][j] != 1e9) col[j] += dist[i][j];
        else col[j] = -1e9;
    }
    int ans = 1e9;
    forn(i, m) {
        ans = (col[i] >= 0) ? min(ans, col[i]) : ans;
        //printf("%d ", col[i]);
    }
    if(ans == 1e9){
        printf("-1\n");
        return 0;
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < (n); i++)

int n, m, dist[105][105], col[105];
string line[105];

int main(){
    scanf("%d %d", &n, &m);
    forn(i, n) cin >> line[i];
    forn(i, 105) forn(j, 105) dist[i][j] = 1e9;
    forn(i, n) forn(j, m){
        if(line[i][j] == '1'){
            dist[i][j] = 0;
            int c = 0;
            for(int k = 1; k <= m; k++){
                if(dist[i][ (j+k)%m ] > dist[i][j] + k){
                    dist[i][ (j+k)%m ] = dist[i][j] + k;
                }
                else break;
            }
            for(int k = 1; k <= m; k++){
                if(dist[i][ ((j-k)+30*m)%m ] > dist[i][j] + k){
                    dist[i][ ((j-k)+30*m)%m ] = dist[i][j] + k;
                }
                else break;
            }
        }
    }
    forn(i, n) forn(j, m){
        if(dist[i][j] != 1e9) col[j] += dist[i][j];
        else col[j] = -1e9;
    }
    int ans = 1e9;
    forn(i, m) {
        ans = (col[i] >= 0) ? min(ans, col[i]) : ans;
        //printf("%d ", col[i]);
    }
    if(ans == 1e9){
        printf("-1\n");
        return 0;
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < (n); i++)

int n, m, dist[105][10005], col[10005];
string line[105];

int main(){
    scanf("%d %d", &n, &m);
    forn(i, n) cin >> line[i];
    forn(i, 105) forn(j, 10005) dist[i][j] = 1e9;
    forn(i, n) forn(j, m){
        if(line[i][j] == '1'){
            dist[i][j] = 0;
            int c = 0;
            for(int k = 1; k <= m; k++){
                if(dist[i][ (j+k)%m ] > dist[i][j] + k){
                    dist[i][ (j+k)%m ] = dist[i][j] + k;
                }
                else break;
            }
            for(int k = 1; k <= m; k++){
                if(dist[i][ ((j-k)+30*m)%m ] > dist[i][j] + k){
                    dist[i][ ((j-k)+30*m)%m ] = dist[i][j] + k;
                }
                else break;
            }
        }
    }
    forn(i, n) forn(j, m){
        if(dist[i][j] != 1e9) col[j] += dist[i][j];
        else col[j] = -1e9;
    }
    int ans = 1e9;
    forn(i, m) {
        ans = (col[i] >= 0) ? min(ans, col[i]) : ans;
        //printf("%d ", col[i]);
    }
    if(ans == 1e9){
        printf("-1\n");
        return 0;
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < (n); i++)

int n, m, dist[105][10005], col[10005];
string line[105];

int main(){
    scanf("%d %d", &n, &m);
    forn(i, n) cin >> line[i];
    forn(i, 105) forn(j, 10005) dist[i][j] = 1e9;
    forn(i, n) forn(j, m){
        if(line[i][j] == '1'){
            dist[i][j] = 0;
            int c = 0;
            for(int k = 1; k <= m; k++){
                if(line[i][(j+k)%m] == '1') break;
                if(dist[i][ (j+k)%m ] > dist[i][j] + k){
                    dist[i][ (j+k)%m ] = dist[i][j] + k;
                }
                else break;
            }
            for(int k = 1; k <= m; k++){
                if(line[i][ ((j-k)+30*m)%m ] == '1') break;
                if(dist[i][ ((j-k)+30*m)%m ] > dist[i][j] + k){
                    dist[i][ ((j-k)+30*m)%m ] = dist[i][j] + k;
                }
                else break;
            }
        }
    }
    forn(i, n) forn(j, m){
        if(dist[i][j] != 1e9) col[j] += dist[i][j];
        else col[j] = -1e9;
    }
    int ans = 1e9;
    forn(i, m) {
        ans = (col[i] >= 0) ? min(ans, col[i]) : ans;
        //printf("%d ", col[i]);
    }
    if(ans == 1e9){
        printf("-1\n");
        return 0;
    }
    printf("%d\n", ans);
    return 0;
}


#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<utility>
#include<iostream>
#include<algorithm>
#include<functional>
#define REP(x,y,z) for(int x=y;x<=z;x++)
#define FORD(x,y,z) for(int x=y;x>=z;x--)
#define MSET(x,y) memset(x,y,sizeof(x))
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 105
#define INF 1000000000
using namespace std;
int n,m,ans,fr[M][M],bk[M][M],last;
char in[M][M];
int get_cost(int x)
{
    int re=0,tmp;
    REP(i,1,n)
    {
        tmp=INF;
        if(~fr[i][x])tmp=min(tmp,min(m-x+fr[i][x],x-fr[i][x]));
        if(~bk[i][x])tmp=min(tmp,min(m-bk[i][x]+x,bk[i][x]-x));
        if(tmp==INF)return INF;
        re+=tmp;
    }
    return re;
}
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        REP(i,1,n)scanf("%s",in[i]+1);

        REP(i,1,n)
        {
            last=-1;
            REP(j,1,m)
            {
                if(in[i][j]=='1')last=j;
                fr[i][j]=last;
            }
        }

        REP(i,1,n)
        {
            last=-1;
            FORD(j,m,1)
            {
                if(in[i][j]=='1')last=j;
                bk[i][j]=last;
            }
        }

        ans=INF;
        REP(j,1,m)ans=min(ans,get_cost(j));
        if(ans==INF)puts("-1");
        else printf("%d\n",ans);
    }
    return 0;
}


#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<utility>
#include<iostream>
#include<algorithm>
#include<functional>
#define REP(x,y,z) for(int x=y;x<=z;x++)
#define FORD(x,y,z) for(int x=y;x>=z;x--)
#define MSET(x,y) memset(x,y,sizeof(x))
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 105
#define INF 1000000000
using namespace std;
int n,m,ans,ll[M],rr[M],fr[M][10005],bk[M][10005],last;
char in[M][10005];
int get_cost(int x)
{
	int re=0,tmp;
	REP(i,1,n)
	{
		tmp=INF;
		if(~fr[i][x])tmp=min(tmp,x-fr[i][x]);
		if(~bk[i][x])tmp=min(tmp,bk[i][x]-x);

		if(~rr[i])tmp=min(tmp,m-rr[i]+1+i-1);
		if(~ll[i])tmp=min(tmp,ll[i]+m-i);

		if(tmp==INF)return INF;
		re+=tmp;
	}
	return re;
}
int main()
{
	while(~scanf("%d %d",&n,&m))
	{
		MSET(ll,-1);
		MSET(rr,-1);
		REP(i,1,n)scanf("%s",in[i]+1);

		REP(i,1,n)
		{
			last=-1;
			REP(j,1,m)
			{
				if(in[i][j]=='1' && ll[i]==-1)ll[i]=j;
				if(in[i][j]=='1')last=j;
				fr[i][j]=last;
			}
		}

		REP(i,1,n)
		{
			last=-1;
			FORD(j,m,1)
			{
				if(in[i][j]=='1' && rr[i]==-1)rr[i]=j;
				if(in[i][j]=='1')last=j;
				bk[i][j]=last;
			}
		}

		ans=INF;
		REP(j,1,m)ans=min(ans,get_cost(j));
		if(ans==INF)puts("-1");
		else printf("%d\n",ans);
	}
	return 0;
}


#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<utility>
#include<iostream>
#include<algorithm>
#include<functional>
#define REP(x,y,z) for(int x=y;x<=z;x++)
#define FORD(x,y,z) for(int x=y;x>=z;x--)
#define MSET(x,y) memset(x,y,sizeof(x))
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 105
#define INF 1000000000
using namespace std;
int n,m,ans,ll[M],rr[M],fr[M][10005],bk[M][10005],last;
char in[M][10005];
int get_cost(int x)
{
	int re=0,tmp;
	REP(i,1,n)
	{
		tmp=INF;
		if(~fr[i][x])tmp=min(tmp,x-fr[i][x]);
		if(~bk[i][x])tmp=min(tmp,bk[i][x]-x);

		if(~rr[i])tmp=min(tmp,m-i+1+i-1);
		if(~ll[i])tmp=min(tmp,i+m-i);

		if(tmp==INF)return INF;
		re+=tmp;
	}
	return re;
}
int main()
{
	while(~scanf("%d %d",&n,&m))
	{
		MSET(ll,-1);
		MSET(rr,-1);
		REP(i,1,n)scanf("%s",in[i]+1);

		REP(i,1,n)
		{
			last=-1;
			REP(j,1,m)
			{
				if(in[i][j]=='1' && ll[i]==-1)ll[i]=j;
				if(in[i][j]=='1')last=j;
				fr[i][j]=last;
			}
		}

		REP(i,1,n)
		{
			last=-1;
			FORD(j,m,1)
			{
				if(in[i][j]=='1' && rr[i]==-1)rr[i]=j;
				if(in[i][j]=='1')last=j;
				bk[i][j]=last;
			}
		}

		ans=INF;
		REP(j,1,m)ans=min(ans,get_cost(j));
		if(ans==INF)puts("-1");
		else printf("%d\n",ans);
	}
	return 0;
}


#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<utility>
#include<iostream>
#include<algorithm>
#include<functional>
#define REP(x,y,z) for(int x=y;x<=z;x++)
#define FORD(x,y,z) for(int x=y;x>=z;x--)
#define MSET(x,y) memset(x,y,sizeof(x))
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 105
#define INF 1000000000
using namespace std;
int n,m,ans,ll[M],rr[M],fr[M][10005],bk[M][10005],last;
char in[M][10005];
int get_cost(int x)
{
    int re=0,tmp;
    REP(i,1,n)
    {
        tmp=INF;
        if(~fr[i][x])tmp=min(tmp,x-fr[i][x]);
        if(~bk[i][x])tmp=min(tmp,bk[i][x]-x);

        if(~rr[i])tmp=min(tmp,m-rr[i]+1+x-1);
        if(~ll[i])tmp=min(tmp,ll[i]+m-x);

        if(tmp==INF)return INF;
        re+=tmp;
    }
    return re;
}
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        MSET(ll,-1);
        MSET(rr,-1);
        REP(i,1,n)scanf("%s",in[i]+1);

        REP(i,1,n)
        {
            last=-1;
            REP(j,1,m)
            {
                if(in[i][j]=='1' && ll[i]==-1)ll[i]=j;
                if(in[i][j]=='1')last=j;
                fr[i][j]=last;
            }
        }

        REP(i,1,n)
        {
            last=-1;
            FORD(j,m,1)
            {
                if(in[i][j]=='1' && rr[i]==-1)rr[i]=j;
                if(in[i][j]=='1')last=j;
                bk[i][j]=last;
            }
        }

        ans=INF;
        REP(j,1,m)ans=min(ans,get_cost(j));
        if(ans==INF)puts("-1");
        else printf("%d\n",ans);
    }
    return 0;
}


#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<utility>
#include<iostream>
#include<algorithm>
#include<functional>
#define REP(x,y,z) for(int x=y;x<=z;x++)
#define FORD(x,y,z) for(int x=y;x>=z;x--)
#define MSET(x,y) memset(x,y,sizeof(x))
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 105
#define INF 1000000000
using namespace std;
int n,m,ans,fr[M][10005],bk[M][10005],last;
char in[M][10005];
int get_cost(int x)
{
    int re=0,tmp;
    REP(i,1,n)
    {
        tmp=INF;
        if(~fr[i][x])tmp=min(tmp,min(m-x+fr[i][x],x-fr[i][x]));
        if(~bk[i][x])tmp=min(tmp,min(m-bk[i][x]+x,bk[i][x]-x));
        if(tmp==INF)return INF;
        re+=tmp;
    }
    return re;
}
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        REP(i,1,n)scanf("%s",in[i]+1);

        REP(i,1,n)
        {
            last=-1;
            REP(j,1,m)
            {
                if(in[i][j]=='1')last=j;
                fr[i][j]=last;
            }
        }

        REP(i,1,n)
        {
            last=-1;
            FORD(j,m,1)
            {
                if(in[i][j]=='1')last=j;
                bk[i][j]=last;
            }
        }

        ans=INF;
        REP(j,1,m)ans=min(ans,get_cost(j));
        if(ans==INF)puts("-1");
        else printf("%d\n",ans);
    }
    return 0;
}