#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int inf = 1e9 + 10;
pair<int, int> seg[2 * N][10];
int n, q, x, typ, l, r;
const pair<int, int> def = {inf, inf};
pair<int, int> merge(pair<int, int>& a, pair<int, int>& b) {
  pair<int, int> res = def;
  res.first = min(a.first, b.first);
  if (a.first < b.first)
    res.second = min(a.second, b.first);
  else
    res.second = min(b.second, a.first);
  return res;
}
int query(int L, int R) {
  pair<int, int> final = def;
  for (int i = 0; i < 10; i++) {
    pair<int, int> ans = def;
    for (int lh = L + n, rh = R + n; lh < rh; lh >>= 1, rh >>= 1) {
      if (lh & 1) ans = merge(ans, seg[lh++][i]);
      if (rh & 1) ans = merge(ans, seg[--rh][i]);
    }
    if (ans.first >= inf || ans.second >= inf) continue;
    if (ans.first + ans.second < final.first + final.second) final = ans;
  }
  if (final.first >= inf || final.second >= inf) return -1;
  return (final.first + final.second);
}
void update(int id, int val) {
  int cop = val, len = 0;
  vector<int> digit(10, 0);
  id += n;
  while (cop) {
    digit[len++] = cop % 10;
    cop /= 10;
  }
  for (int i = 0; i < 10; i++) {
    seg[id][i] = def;
    if (digit[i] > 0) seg[id][i].first = val;
    for (int j = id; j >= 1; j >>= 1)
      seg[j >> 1][i] = merge(seg[j][i], seg[j ^ 1][i]);
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> x;
    update(i, x);
  }
  while (q--) {
    cin >> typ >> l >> r;
    if (typ == 1)
      update(l - 1, r);
    else
      cout << query(l - 1, r) << "\n";
  }
}
