#include <bits/stdc++.h>
using namespace std;
const int NMax = 100005;
int N, M;
int A[NMax];
long long Sum[10];
struct Node {
  long long sum[10];
  int next[10];
} Tree[4 * NMax];
void combineNodes(int K) {
  for (int i = 0; i <= 9; i++)
    Tree[K].sum[i] = Tree[K * 2].sum[i] + Tree[K * 2 + 1].sum[i];
}
void buildTree(int K, int L, int R) {
  if (L == R) {
    int pw = 1, nb = A[L];
    while (nb != 0) {
      Tree[K].sum[nb % 10] += pw;
      nb /= 10;
      pw *= 10;
    }
    for (int i = 0; i <= 9; i++) Tree[K].next[i] = i;
    return;
  }
  for (int i = 0; i <= 9; i++) Tree[K].next[i] = i;
  buildTree(K * 2, L, (L + R) / 2);
  buildTree(K * 2 + 1, (L + R) / 2 + 1, R);
  combineNodes(K);
}
void propagate(int K, int L, int R) {
  if (L != R) {
    for (int i = 0; i <= 9; i++) {
      Tree[K * 2].next[i] = Tree[K].next[Tree[K * 2].next[i]];
      Tree[K * 2 + 1].next[i] = Tree[K].next[Tree[K * 2 + 1].next[i]];
    }
  }
  for (int i = 0; i <= 9; i++) Sum[i] = 0;
  for (int i = 0; i <= 9; i++) {
    Sum[Tree[K].next[i]] += Tree[K].sum[i];
  }
  for (int i = 0; i <= 9; i++) {
    Tree[K].sum[i] = Sum[i];
    Tree[K].next[i] = i;
  }
}
void Update(int K, int L, int R, int x, int y, int from, int to) {
  propagate(K, L, R);
  if (L > R || L > y || R < x) return;
  if (L >= x && R <= y) {
    Tree[K].next[from] = to;
    propagate(K, L, R);
    return;
  }
  Update(K * 2, L, (L + R) / 2, x, y, from, to);
  Update(K * 2 + 1, (L + R) / 2 + 1, R, x, y, from, to);
  combineNodes(K);
}
long long Query(int K, int L, int R, int x, int y) {
  propagate(K, L, R);
  if (L > R || L > y || R < x) return 0;
  long long sum = 0;
  if (L >= x && R <= y) {
    for (int i = 0; i <= 9; i++) sum += 1LL * i * Tree[K].sum[i];
    return sum;
  }
  long long a = Query(K * 2, L, (L + R) / 2, x, y);
  long long b = Query(K * 2 + 1, (L + R) / 2 + 1, R, x, y);
  return a + b;
}
void Solve() {
  for (int i = 1; i <= M; i++) {
    int type, l, r, x, y;
    scanf("%d", &type);
    if (type == 1) {
      scanf("%d%d%d%d", &l, &r, &x, &y);
      Update(1, 1, N, l, r, x, y);
    } else {
      scanf("%d%d", &l, &r);
      printf("%I64d\n", Query(1, 1, N, l, r));
    }
  }
}
void Read() {
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
}
int main() {
  Read();
  buildTree(1, 1, N);
  Solve();
  return 0;
}
